I'm implementing a simple xml parser and faced with a problem:
exception is thrown when initialising JAXBContext.
Here is my code(maybe too much for this question, but I've tried to include only important parts):
Parser itself:
package com.andreiyusupau.jaxbparser.parser;
import com.andreiyusupau.jaxbparser.model.Parent;
import com.andreiyusupau.jaxbparser.model.Root;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.util.Collection;
public class JaxbParser {
public Collection<Parent> parse(String inputXml) throws JAXBException {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Root root = (Root) unmarshaller.unmarshal(new File(inputXml));
return root.getElements();
}
}
Main:
public class Main {
public static final String FILE_NAME = "data.xml";
public static void main(String[] args) throws JAXBException {
JaxbParser jaxbParser = new JaxbParser();
Collection<Parent> elements= jaxbParser.parse(FILE_NAME);
for (Parent element : elements) {
System.out.println(element.toString());
}
}
}
Classes (getters,setters and toString are hidden):
import javax.xml.bind.annotation.*;
import java.util.List;
#XmlRootElement(name = "root")
#XmlAccessorType(XmlAccessType.FIELD)
public class Root {
#XmlElements({
#XmlElement(name = "ChildA", type = ChildA.class),
#XmlElement(name = "ChildB", type = ChildB.class),
})
private List<Parent> elements;
public Root() {
}
}
import org.eclipse.persistence.oxm.annotations.XmlPath;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlTransient;
#XmlTransient
#XmlSeeAlso({ChildA.class, ChildB.class})
public class Parent {
#XmlAttribute
private int id;
#XmlElement(name = "name")
private String name;
#XmlElement(name = "parameter")
private String parameter;
#XmlPath("parameter/#parameterAttr")
private String parameterAttribute;
#XmlPath("similar/a/text()")
private int a;
#XmlPath("similar/b/text()")
private int b;
#XmlPath("different/similar/text()")
private String similar;
#XmlPath("different/similar/#similarAttr")
private String similarAttribute;
public Parent() {
}
}
import org.eclipse.persistence.oxm.annotations.XmlPath;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "childA")
public class ChildA extends Parent {
#XmlPath("different/differentA/text()")
private String differentA;
#XmlPath("different/differentA/#attrA")
private String differentAttributeA;
public ChildA() {
}
}
import org.eclipse.persistence.oxm.annotations.XmlPath;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "childB")
public class ChildB extends Parent {
#XmlPath("different/differentB1/text()")
private String differentB1;
#XmlPath("different/differentB1/#attrB1")
private String differentAttributeB1;
#XmlPath("different/differentB2/text()")
private String differentB2;
#XmlPath("different/differentB2/#attrB2")
private String differentAttributeB2;
public ChildB() {
}
}
and finally an xml file itself:
<?xml version="1.1" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation = "schema.xsd">
<childA id="ID-1">
<name>Name</name>
<parameter parameterAttr="a">b</parameter>
<similarGroup>
<a>1</a>
<b>2</b>
</similarGroup>
<different>
<similar similarAttr="n">2</similar>
<differentA attrA="n">100</differentA>
</different>
</childA>
<childB id="ID-2">
<name>Name</name>
<parameter parameterAttr="c">d</parameter>
<similarGroup>
<a>6</a>
<b>8</b>
</similarGroup>
<different>
<similar similarAttr="g">7</similar>
<differentB1 attrB1="5">100</differentB1>
<differentB2>100</differentB2>
</different>
</childB>
</root>
and dependencies in POM xml:
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>3.0.0-M1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3</version>
</dependency>
jaxb.properties
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Compiling and running this code gives the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: jakarta/xml/bind/JAXBContext
at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3166)
at java.base/java.lang.Class.getMethodsRecursive(Class.java:3307)
at java.base/java.lang.Class.getMethod0(Class.java:3293)
at java.base/java.lang.Class.getMethod(Class.java:2106)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:249)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:240)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:345)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632)
at com.andreiyusupau.jaxbparser.parser.JaxbParser.parse(JaxbParser.java:15)
at com.andreiyusupau.jaxbparser.Main.main(Main.java:15)
Caused by: java.lang.ClassNotFoundException: jakarta.xml.bind.JAXBContext
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 12 more
Maybe someone knows how can I solve it?
UPDATE:
After replacing jakarta.xml.bind-api with jaxb-api I have the following exception:
Exception in thread "main" javax.xml.bind.JAXBException: ClassCastException: attempting to cast jar:file:/C:/Users/Nevermind/.m2/repository/javax/xml/bind/jaxb-api/2.3.1/jaxb-api-2.3.1.jar!/javax/xml/bind/JAXBContext.class to jar:file:/C:/Users/Nevermind/.m2/repository/javax/xml/bind/jaxb-api/2.3.1/jaxb-api-2.3.1.jar!/javax/xml/bind/JAXBContext.class. Please make sure that you are specifying the proper ClassLoader.
at javax.xml.bind.ContextFinder.handleClassCastException(ContextFinder.java:157)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:300)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:286)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:391)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
at com.andreiyusupau.jaxbparser.parser.JaxbParser.parse(JaxbParser.java:15)
at com.andreiyusupau.jaxbparser.Main.main(Main.java:15)
The exception is thrown in line:
JAXBContext jc = JAXBContext.newInstance(Root.class);
Any suggestions?
I suggest replace dependency
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
with
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
jakarta.xml.bind comes from Jakarta EE and org.glassfish.jaxb comes from older Java EE.
Do mvn dependency:tree (or mvn help:effective-pom) to see which dependencies end up on your classpath.
Try using these:
<!-- XML -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>2.3.3</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<scope>runtime</scope>
<version>2.3.3</version>
</dependency>
All legacy JavaEE javax.* API's are being cleaned up by JakartaEE and released again as jakarta.* API's with proper maven metadata.
Related
There is an application that is configured by hk2 dependency injection.
the pom.xml :
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<java.version>17</java.version>
<junit.version>5.4.0</junit.version>
<jsonassert.version>1.5.0</jsonassert.version>
<jersey.version>3.0.2</jersey.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Grizzly 2 HTTP Server -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<!-- Jersey DI and core-->
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-metadata-generator</artifactId>
<version>3.0.2</version>
</dependency>
<!-- add jackson as json provider -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<!-- Need this to hide warning for jakarta.activation.DataSource -->
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.0.1</version>
</dependency>
<!-- JUnit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- test json data -->
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>${jsonassert.version}</version>
<scope>test</scope>
</dependency>
and MyResource.java:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mkyong.json.service.MessageService;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
#Path("/hello")
public class MyResource {
private static final ObjectMapper mapper = new ObjectMapper();
#Inject
#Named("aws")
private MessageService awsService;
#Inject
#Named("azure")
private MessageService azureService;
#Path("/hk2/aws")
#GET
#Produces(MediaType.TEXT_PLAIN)
public String helloAws() {
String result = awsService.getHello();
ObjectNode json = mapper.createObjectNode();
return result;
}
#Path("/hk2/azure")
#GET
#Produces(MediaType.TEXT_PLAIN)
public String helloAzure() {
return azureService.getHello();
}
}
and AutoScanFeature.java :
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Feature;
import jakarta.ws.rs.core.FeatureContext;
import org.glassfish.hk2.api.DynamicConfigurationService;
import org.glassfish.hk2.api.MultiException;
import org.glassfish.hk2.api.Populator;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.utilities.ClasspathDescriptorFileFinder;
import org.glassfish.hk2.utilities.DuplicatePostProcessor;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class AutoScanFeature implements Feature {
#Inject
ServiceLocator serviceLocator;
#Override
public boolean configure(FeatureContext context) {
DynamicConfigurationService dcs =
serviceLocator.getService(DynamicConfigurationService.class);
Populator populator = dcs.getPopulator();
try {
// Populator - populate HK2 service locators from inhabitants files
// ClasspathDescriptorFileFinder - find files from META-INF/hk2-locator/default
populator.populate(
new ClasspathDescriptorFileFinder(this.getClass().getClassLoader()),
new DuplicatePostProcessor());
} catch (IOException | MultiException ex) {
Logger.getLogger(AutoScanFeature.class.getName()).log(Level.SEVERE, null, ex);
}
return true;
}
}
Service interface:
#Contract
public interface UserService {
void add(User user);
}
and its implementation:
#Service
public class UserServiceImpl implements UserService {
#Override
public void add(User user) {
super.save(user, hibernateOracleTestXML);
}
}
Everything is working correctly and dependency injection is only worked by only #Service and #Contract, but I want to use #Singleton or something else for such as DAO layer.
Another hand It does not work when I use #Singleton or something else except #Service
How to solve this problem?
The #Contract and #Service are both for its default injection, according to the question, these annotations are not suitable for DAO layer, so I solved this issue by org.glassfish.hk2.utilities.binding.AbstractBinder like this:
#Override
protected void configure() {
Reflections reflections = new Reflections(packageName);
Set<Class<?>> repositories = reflections.getTypesAnnotatedWith(Repository.class, true);
repositories.stream().forEach(repo -> {
BeanAddress beanAddresses = repo.getAnnotationsByType(BeanAddress.class)[0];
try {
Class<?> requiredClass = Class.forName(beanAddresses.implPackageName());
bind(requiredClass).to(repo)
.named(Class.class.getCanonicalName()).in(Singleton.class);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
});
...
As it shows, I manually bind the interface to its class of DAO layers by an annotation that is made by myself(BeanAddress.class)
I'm creating a Publisher Spring boot application to test sending an Account object using jmsTemplate.convertAndSend(destination, account); to a Consumer Spring boot application I created.
In my pom.xml I have the following <dependencies> and <plugin> setup which lets maven-jaxb2-plugin generate the Java object from the xsd folder I created.
pom.xml
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-artemis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-oxm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!-- user-added -->
<plugin>
<!-- https://mvnrepository.com/artifact/org.jvnet.jaxb2.maven2/maven-jaxb2-plugin -->
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.15.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${project.basedir}/src/main/resources/xsd</schemaDirectory>
<schemaIncludes>
<include>*.xsd</include>
</schemaIncludes>
</configuration>
</plugin>
</plugins>
</build>
MessageProducerApplication
import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jms.core.JmsTemplate;
import com.example.xmlns.Account;
#SpringBootApplication
public class MessageProducerApplication implements CommandLineRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(MessageProducerApplication.class.getName());
#Autowired
private JmsTemplate jmsTemplate;
#Value("${outbound.queue}")
private String destination; // outbound queue
public static void main(String[] args) {
SpringApplication.run(MessageProducerApplication.class, args);
}
#Override
public void run(String... args) throws Exception {
ClassPathResource xmlResource = new ClassPathResource("xml/accountcreate.xml");
InputStream xmlStream = ClassLoader.getSystemResourceAsStream(xmlResource.getPath());
File xmlFile = new File("src/main/resources/xml/accountcreate.xml");
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Account.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Account account = (Account) jaxbUnmarshaller.unmarshal(xmlFile);
//Account account = (Account) jaxbUnmarshaller.unmarshal(xmlStream);
jmsTemplate.convertAndSend(destination, account);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Problem
The conversion to Account type fails in this line:
Account account = (Account) jaxbUnmarshaller.unmarshal(xmlFile);
Exception
org.springframework.jms.support.converter.MessageConversionException: Cannot convert object of type [com.example.xmlns.Account] to JMS message. Supported message payloads are: String, byte array, Map<String,?>, Serializable object.
at org.springframework.jms.support.converter.SimpleMessageConverter.toMessage(SimpleMessageConverter.java:79) ~[spring-jms-5.3.19.jar:5.3.19]
The Account.java class was generated by xjc (both my Producer and Consumer application has the same Account class)
#XmlRootElement(name = "account")
public class Account {
#XmlElement(required = true)
//fields....
}
In my Consumer application, I set the listener to receive a type of Account
#Component
public class MessageListener {
private static final Logger LOGGER = LoggerFactory.getLogger(MessageListener.class);
#JmsListener(destination = "${inbound.queue}")
public void onMessage(Account account) {
LOGGER.info("account"+account);
}
}
I'd appreciate any thoughts or suggestion. Thanks.
Okay, after reviewing my code I found the reason why it's failing to cast to Account after unmarshal(). I realized, I wasn't using the Jaxb2Marshaller that I configured with a MarshallingMessageConverter in my #Configuration class.
The changes to be made on MessageProducerApplication class are as follows
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
#Autowired
private Jaxb2Marshaller jaxb2Marshaller; //inject this to perform unmarshalling based on configuration in JmsConfig class
File xmlFile = xmlResource.getFile();
try {
Account account = (Account) jaxb2Marshaller.unmarshal(new StreamSource(xmlFile));
jmsTemplate.convertAndSend(destination, account);
} catch (Exception e) {
e.printStackTrace();
}
Then add the configuration class
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.Marshaller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jms.support.converter.MarshallingMessageConverter;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
#Configuration
public class JmsConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(JmsConfig.class.getName());
#Bean
public Jaxb2Marshaller createJaxb2Marshaller(#Value("${javaobject.ctxpath}") final String contextPath) {
ClassPathResource resource = new ClassPathResource("xsd/account.xsd");
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setContextPath(contextPath);
jaxb2Marshaller.setSchema(resource);
Map<String, Object> properties = new HashMap<>();
properties.put(Marshaller.JAXB_FORMATTED_OUTPUT, true);// pretty print the xml
jaxb2Marshaller.setMarshallerProperties(properties);
return jaxb2Marshaller;
}
#Bean
public MarshallingMessageConverter createMashallingMessageConverter(final Jaxb2Marshaller jaxb2Marshaller) {
return new MarshallingMessageConverter(jaxb2Marshaller);
}
}
I hope this would help others who might encounter the same problem.
API Requests using Postman
I tried to send HTTP requests to the java backend web server application and perform CRUD operations. GET operations are working fine and gave back the proper response but save, update and delete operations gave the following error.
Please note that there are three customer records in my database and I tried to send HTTP requests as follows;
GET http://localhost:8080/tms/api/v1/customers ---> Successful ✔
GET http://localhost:8080/tms/api/v1/customers/3 ---> Successful ✔
DELETE http://localhost:8080/tms/api/v1/customers/3 ---> Unsuccessful ❌
Error Log
Customer ID: 2
02-Feb-2022 09:59:11.271 SEVERE [http-nio-8080-exec-2] com.myapp.web.api.exceptionhandler.AppWideExceptionHandler.globalExceptionHandler null
javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:295)
at com.sun.proxy.$Proxy80.remove(Unknown Source)
at com.myapp.web.dal.CrudDAOImpl.delete(CrudDAOImpl.java:67)
at com.myapp.web.business.custom.impl.CustomerBOImpl.deleteCustomer(CustomerBOImpl.java:79)
at com.myapp.web.api.CustomerController.deleteCustomer(CustomerController.java:116)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doDelete(FrameworkServlet.java:931)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:359)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1735)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
API Layer
CustomerController.java
package com.myapp.web.api;
import com.myapp.web.api.util.ApiUtil;
import com.myapp.web.business.custom.CustomerBO;
import com.myapp.web.dto.CustomerDTO;
import com.myapp.web.exception.IdFormatException;
import com.myapp.web.exception.RecordNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.List;
#CrossOrigin(origins = "http://localhost:8080")
#RequestMapping("/api/v1/customers")
#RestController
public class CustomerController {
#Autowired
private CustomerBO customerBO;
/**
* Get all customers list.
*
* #return List<CustomerDTO> customersList.
*/
#GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<CustomerDTO> getAllCustomers() throws Exception {
return customerBO.getAllCustomers();
}
/**
* Get customer by customer ID.
*
* #return CustomerDTO customer object.
* #throws IdFormatException if the ID is not an Integer.
* #throws RecordNotFoundException if matching customer record not found,
*/
#GetMapping(produces = MediaType.APPLICATION_JSON_VALUE,
value = "/{id:\\d}")
public CustomerDTO getCustomerByID(#PathVariable(name = "id") String id) throws Exception {
System.out.println("CustomerID: " + id);
Integer customerID = ApiUtil.getIntegerId(id);
CustomerDTO customer = customerBO.getCustomerByID(customerID);
System.out.println("Customer Result: " + customer);
/* If customer not found. */
if (customer == null) throw new RecordNotFoundException();
return customer;
}
/**
* Delete customer by Customer ID.
*/
#ResponseStatus(HttpStatus.NO_CONTENT)
#DeleteMapping(value = "/{id:\\d}")
public void deleteCustomer(#PathVariable String id) throws Exception {
Integer customerID = ApiUtil.getIntegerId(id);
System.out.println("Customer ID: " + customerID);
customerBO.deleteCustomer(customerID);
}
}
Business Layer
SuperBO.java
package com.myapp.web.business;
public interface SuperBO {
}
CustomerBOImpl.java
package com.myapp.web.business.custom.impl;
import com.myapp.web.business.custom.CustomerBO;
import com.myapp.web.business.custom.util.mapper.CustomerDTOMapper;
import com.myapp.web.dal.custom.CustomerDAO;
import com.myapp.web.dto.CustomerDTO;
import com.myapp.web.entity.Customer;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
#NoArgsConstructor
#Transactional
#Service
public class CustomerBOImpl implements CustomerBO {
#Autowired
private CustomerDAO customerDAO;
#Autowired
private CustomerDTOMapper mapper;
#Override
public void deleteCustomer(int customerID) throws Exception {
/* delete. */
this.customerDAO.delete(customerID);
}
#Transactional(readOnly = true)
#Override
public CustomerDTO getCustomerByID(int customerID) throws Exception {
/* get customer by customer ID. */
return this.mapper.getCustomerDTO(this.customerDAO.get(customerID));
}
#Transactional(readOnly = true)
#Override
public List<CustomerDTO> getAllCustomers() throws Exception {
/* get all customers. */
return this.mapper.getCustomerDTOs(this.customerDAO.getAll());
}
}
CustomerDTOMapper.java
package com.myapp.web.business.custom.util.mapper;
import com.myapp.web.dto.CustomerDTO;
import com.myapp.web.entity.Customer;
import org.mapstruct.Mapper;
import java.util.List;
#Mapper(componentModel = "spring")
public abstract class CustomerDTOMapper {
/* -------------------- Customer -------------------- */
public abstract Customer getCustomer(CustomerDTO customerDTO);
public abstract CustomerDTO getCustomerDTO(Customer customer);
public abstract List<CustomerDTO> getCustomerDTOs(List<Customer> customerList);
}
Data Access Layer
SuperDAO.java
package com.myapp.web.dal;
import javax.persistence.EntityManager;
public interface SuperDAO {
EntityManager getEntityManager();
}
CrudDAO.java
package com.myapp.web.dal;
import com.myapp.web.entity.SuperEntity;
import java.io.Serializable;
import java.util.List;
public interface CrudDAO<T extends SuperEntity<Serializable>, PK extends Serializable> extends SuperDAO {
/**
* saving an entity and return the entity.
*
* #return T SuperEntity. */
T save(T entity) throws Exception;
void update(T entity) throws Exception;
void delete(PK key) throws Exception;
T get(PK key) throws Exception;
List<T> getAll() throws Exception;
}
CrudDAOImpl.java
package com.myapp.web.dal;
import com.myapp.web.entity.SuperEntity;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
public class CrudDAOImpl<T extends SuperEntity<Serializable>, K extends Serializable> implements CrudDAO<T, K> {
private final Class<T> entityClass;
#PersistenceContext
private EntityManager entityManager;
public CrudDAOImpl() {
this.entityClass =
(Class<T>) (((ParameterizedType) (this.getClass().getGenericSuperclass())).getActualTypeArguments()[0]);
}
/**
* This method is used to pass the EntityManager to the lower level classes that extend the CrudDAOImpl class.
*/
public EntityManager getEntityManager() {
return this.entityManager;
}
#Override
public void delete(K key) throws Exception {
this.entityManager.remove(key);
}
#Override
public T get(K key) throws Exception {
return this.entityManager.find(this.entityClass, key);
}
#Override
public List<T> getAll() throws Exception {
List<T> resultList = (List<T>) this.entityManager.
createQuery("SELECT e FROM " + this.entityClass.getName() + " e").getResultList();
return resultList;
}
}
CustomerDAO.java
package com.myapp.web.dal.custom;
import com.myapp.web.dal.CrudDAO;
import com.myapp.web.entity.Customer;
public interface CustomerDAO extends CrudDAO<Customer, Integer> {
}
CustomerDAOImpl.java
package com.myapp.web.dal.custom.impl;
import com.myapp.web.dal.CrudDAOImpl;
import com.myapp.web.dal.custom.CustomerDAO;
import com.myapp.web.entity.Customer;
import org.springframework.stereotype.Repository;
#Repository
public class CustomerDAOImpl extends CrudDAOImpl<Customer, Integer> implements CustomerDAO {
}
Here's what I have tried.
I tried unit testing. BO layer and DAO layer unit test case worked as intended but when I check the API by sending HTTP request using Postman, save, update and delete cannot be performed using the API.
CustomerDAOImplTest.java ----> TEST PASSED ! ✔
package com.myapp.web.dal.custom.impl;
import com.myapp.web.WebAppConfig;
import com.myapp.web.WebRootConfig;
import com.myapp.web.dal.custom.CustomerDAO;
import com.myapp.web.entity.Customer;
import com.myapp.web.entity.enumeration.GenderTypes;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import static org.junit.Assert.*;
#RunWith(SpringJUnit4ClassRunner.class)
#WebAppConfiguration
#ContextConfiguration(classes = {WebRootConfig.class, WebAppConfig.class})
public class CustomerDAOImplTest {
#Autowired
private CustomerDAO customerDAO;
#Test
public void checkCustomerDAO() {
assertNotNull(customerDAO);
}
#Transactional
#Test
public void getCustomerByID() throws Exception {
assertNotNull(customerDAO);
Customer customer = this.customerDAO.get(10);
System.out.println("GET Customer Entity: " + customer);
assertNotNull(customer);
}
#Test
public void getAllCustomers() throws Exception {
List<Customer> customersList = this.customerDAO.getAll();
assertEquals(2, customersList.size());
}
#Transactional
#Test
public void deleteCustomer() throws Exception {
assertNotNull(this.customerDAO);
/* delete */
this.customerDAO.delete(3);
Customer customerFromDB = this.customerDAO.get(10);
assertNull(customerFromDB);
}
}
CustomerBOImpl.java ----> TEST PASSED ! ✔
package com.myapp.web.business.custom.impl;
import com.myapp.web.WebAppConfig;
import com.myapp.web.WebRootConfig;
import com.myapp.web.business.custom.CustomerBO;
import com.myapp.web.dto.CustomerDTO;
import com.myapp.web.entity.enumeration.GenderTypes;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
#RunWith(SpringJUnit4ClassRunner.class)
#WebAppConfiguration
#ContextConfiguration(classes = {WebRootConfig.class, WebAppConfig.class})
public class CustomerBOImplTest {
#Autowired
private CustomerBO customerBO;
#Test
public void checkCustomerBO() {
assertNotNull(customerBO);
}
// #Transactional
#Test
public void deleteCustomer() throws Exception {
assertNotNull(this.customerBO);
/* delete */
this.customerBO.deleteCustomer(3);
CustomerDTO customerDTOFromDB = this.customerBO.getCustomerByID(10);
assertNull(customerDTOFromDB);
}
}
pom.xml dependencies
<dependencies>
<!-- Servlet-API -->
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- MySQL connector -->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
<scope>compile</scope>
</dependency>
<!-- SLF4J for logging-->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>2.0.0-alpha1</version>
<scope>compile</scope>
</dependency>
<!-- Junit4 for unit testing -->
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- Apache Commons Validator -->
<!-- https://mvnrepository.com/artifact/commons-validator/commons-validator -->
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.7</version>
</dependency>
<!-- mapstruct -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<!-- Hibernate -->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.3.Final</version>
</dependency>
<!-- Apache Commons Codecs -->
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
<!-- Spring Data Access ==================================================================== -->
<!--Spring ORM -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.14</version>
</dependency>
<!-- Apache Commons DBCP -->
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
<!-- Spring Data Access - Spring AOP - Aspectj ========================================== -->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
<scope>runtime</scope>
</dependency>
<!-- Spring Web MVC =================================================================== -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.15</version>
</dependency>
<!-- JACKSON DATABIND -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
</dependency>
<!--Jackson Datatype-->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jsr310 -->
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.1</version>
</dependency>
<!--Spring TestContext Framework-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.15</version>
<scope>test</scope>
</dependency>
</dependencies>
Development Enviroment
Java 8
Tomcat 9
I refer to these following questions and answers but, I could not fix the issue. I would appreciate some help to fix the
error and, understand the error.
https://stackoverflow.com/a/32552558/12898581
javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread
https://www.wilfriedbarth.com/til/2018-03-31-spring-transactional-annotation/
Exception is:
An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'machineController': Unsatisfied dependency expressed through field 'machineRepository': Error creating bean with name 'machineRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property machine found for type Machine!; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'machineRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property machine found for type Machine!
Editor for coding, is Subline, Code for the project
main
Main class for springboot application
package com.brommarest;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
importorg.springframework.data.jpa.repository.config.EnableJpaRepositories;
#SpringBootApplication
#EntityScan(basePackages = {"com.brommarest.entities" })
#EnableJpaRepositories(basePackages = {"com.brommarest.repositories"})
public class BrommaRestApplication {
public static void main(String[] args) {
SpringApplication.run(BrommaRestApplication.class, args);
}
}
Repositories repositoty file, MachineRepository.java
Repository for the Machine entity
package com.brommarest.repositories;
import com.brommarest.entities.Machine;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
import java.util.Set;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
#Repository
public interface MachineRepository extends JpaRepository<Machine, Integer> {
// custom query to search to machine by type or description
List<Machine> findBymachine_typeOrmachine_desc(String text, String
textAgain);
}
entities entity filem, Machine.java
entity class for the repository
package com.brommarest.entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
#Entity
public class Machine {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int machine_id;
private String machine_type;
private String machine_desc;
private String date_added;
public Machine() { }
public Machine(String machine_type, String machine_desc, String date_added)
{
this.setTitle(machine_type);
this.setDesc(machine_desc);
this.setDate(date_added);
}
public Machine(int machine_id, String machine_type, String machine_desc,
String date_added) {
this.setId(machine_id);
this.setTitle(machine_type);
this.setDesc(machine_desc);
this.setDate(date_added);
}
public int getId() {
return machine_id;
}
public void setId(int machine_id) {
this.machine_id = machine_id;
}
public String getTitle() {
return machine_type;
}
public void setTitle(String machine_type) {
this.machine_type = machine_type;
}
public String getDesc() {
return machine_desc;
}
public void setDesc(String machine_desc) {
this.machine_desc = machine_desc;
}
public String getDate() {
return date_added;
}
public void setDate(String date_added) {
this.date_added = date_added;
}
#Override
public String toString() {
return "Machine{" +
"machine_id=" + machine_id +
", machine_type='" + machine_type + '\'' +
", machine_desc='" + machine_desc + '\'' +
", date_added='" + date_added + '\'' +
'}';
}
}
Controller MachineController.java
Controller for the project
package com.brommarest.controllers;
import com.brommarest.entities.Machine;
import com.brommarest.repositories.MachineRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
#RestController
public class MachineController {
#Autowired
MachineRepository machineRepository;
#GetMapping("/machine")
public List<Machine> index(){
return machineRepository.findAll();
}
#GetMapping("/machine/{machine_id}")
public Machine show(#PathVariable String machine_id){
int machineId = Integer.parseInt(machine_id);
return machineRepository.findOne(machineId);
}
#PostMapping("/machine/search")
public List<Machine> search(#RequestBody Map<String, String> body){
String searchTerm = body.get("text");
return machineRepository.findBymachine_typegOrmachine_desc(searchTerm,
searchTerm);
}
#PostMapping("/machine")
public Machine create(#RequestBody Map<String, String> body){
String machine_type = body.get("machine_type");
String machine_desc = body.get("machine_desc");
String date_added = body.get("date_added");
return machineRepository.save(new Machine(machine_type, machine_desc,
date_added));
}
#PutMapping("/machine/{machine_id}")
public Machine update(#PathVariable String machine_id, #RequestBody
Map<String, String> body){
int machineId = Integer.parseInt(machine_id);
// getting machine
//Machine machine = MachineRepository.findOne(machineId);
Machine machine = machineRepository.findOne(machineId);
machine.setTitle(body.get("machine_type"));
machine.setDesc(body.get("machine_desc"));
return machineRepository.save(machine);
}
#DeleteMapping("/machine/{machine_id}")
public boolean delete(#PathVariable String machine_id){
int machineId = Integer.parseInt(machine_id);
machineRepository.delete(machineId);
return true;
}
}
application.properties
spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.jpa.show-sql=true
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=default
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
pom file
<groupId>com.brommarest</groupId>
<artifactId>brommarest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Bromma-REST</name>
<description>Bromma-REST API'S project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-social-twitter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-
beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Interface signature should follow name convention. Spring generates queries based on the method signature and it cannot.
I guess you should capitalize property names. Please try this:
List<Machine> findByMachine_typeOrMachine_desc(String text, String
textAgain);
No underscore ("_") business is Java, unless you know what exactly you are doing. Rename all the methods and field names to use camelCase. Spring is expecting things to be in a certain way, otherwise it will misbehave.
#GetMapping("/machine/{machine_id}") this "_" is fine
But this public Machine show(#PathVariable String machine_id){ is not
I have an XML file that I am trying to read that has elements with attributes. I have tried from multiple examples, but the fields in my class always end up as null as shown below:
Data [type=null, value=null]
Data [type=null, value=null]
Data [type=null, value=null]
Below is the cut down example code of my issue.
Here is an example XML file located in the src/main/resources/data directory (data.xml):
<?xml version="1.0" encoding="UTF-8"?>
<list>
<data type="shopping" value="milk" />
<data type="shopping" value="eggs" />
<data type="TODO" value="return books to library" />
</list>
Below is my domain class for the XML data (Data.java):
package com.example.demo.springbatchtest.domain;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "data")
public class Data {
private final String type;
private final String value;
public Data(String type, String value) {
this.type = type;
this.value = value;
}
#XmlAttribute(name = "type")
public String getType() {
return type;
}
#XmlAttribute(name = "value")
public String getValue() {
return value;
}
#Override
public String toString() {
return "Data [type=" + type + ", value=" + value + "]";
}
}
Here is my Spring Batch job configuration file (JobConfiguration.java):
package com.example.demo.springbatchtest.configuration;
import java.util.HashMap;
import java.util.Map;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.xml.StaxEventItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.oxm.xstream.XStreamMarshaller;
import com.example.demo.springbatchtest.domain.Data;
#Configuration
public class JobConfiguration {
#Autowired
public JobBuilderFactory jobBuilderFactory;
#Autowired
StepBuilderFactory stepBuilderFactory;
#Bean
public StaxEventItemReader<Data> dataItemReader() {
XStreamMarshaller unmarshaller = new XStreamMarshaller();
Map<String, Class> aliases = new HashMap<>();
aliases.put("data", Data.class);
unmarshaller.setAliases(aliases);
StaxEventItemReader<Data> reader = new StaxEventItemReader<>();
reader.setResource(new ClassPathResource("/data/data.xml"));
reader.setFragmentRootElementName("data");
reader.setUnmarshaller(unmarshaller);
return reader;
}
#Bean
public ItemWriter<Data> dataItemWriter() {
return items -> {
for (Data item : items) {
System.out.println(item.toString());
}
};
}
#Bean
public Step step1() {
return stepBuilderFactory.get("step1")
.<Data, Data>chunk(10)
.reader(dataItemReader())
.writer(dataItemWriter())
.build();
}
#Bean
public Job job() {
return jobBuilderFactory
.get("job")
.start(step1())
.build();
}
}
Here is my main Spring Boot class (SpringBatchTestApplication.java):
package com.example.demo.springbatchtest;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
#EnableBatchProcessing
public class SpringBatchTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBatchTestApplication.class, args);
}
}
Here is my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>SpringBatchTestSpringBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBatchTestSpringBoot</name>
<description>Spring Batch Test with Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
It sounds like JAX-B is unable to set the Data.type and value attributes.
By default it will be looking for a getter/setter pair for each attribute, otherwise they will be treated as read-only.
The alternative would be to use field level access - #XmlAccessorType(XmlAccessType.FIELD)
Found this tutorial which helped me get the example code to work.
https://walkingtechie.blogspot.com/2017/03/spring-batch-xml-file-to-mysql-example.html
The key was to a Converter class to parse out the attributes. So in my example, I added a call set a Converter class in the dataItemReader() in the JobConfiguration class.
import com.example.demo.springbatchtest.converter.DataConverter;
...
#Autowired
private DataConverter dataConverter;
...
#Bean
public StaxEventItemReader<Data> dataItemReader() {
XStreamMarshaller unmarshaller = new XStreamMarshaller();
Map<String, Class> aliases = new HashMap<>();
aliases.put("data", Data.class);
unmarshaller.setAliases(aliases);
unmarshaller.setConverters(dataConverter); // from Walking Techie
StaxEventItemReader<Data> reader = new StaxEventItemReader<>();
reader.setResource(new ClassPathResource("/data/data.xml"));
reader.setFragmentRootElementName("data");
reader.setUnmarshaller(unmarshaller);
return reader;
}
And then I extended the Converter class to handle the attributes.
package com.example.demo.springbatchtest.converter;
import org.springframework.stereotype.Component;
import com.example.demo.springbatchtest.domain.Data;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
#Component
public class DataConverter implements Converter {
#Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
}
#Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
String type = reader.getAttribute("type");
String value = reader.getAttribute("value");
return new Data(type, value);
}
#Override
public boolean canConvert(Class type) {
return type.equals(Data.class);
}
}
The program now outputs the following:
Data [type=shopping, value=milk]
Data [type=shopping, value=eggs]
Data [type=TODO, value=return books to library]