I'm working on a project (spring boot) and I have to convert xml file to Java classes using the maven jaxb2 plugin. I'm following this link: the classes are generated the problem is when I try to unmarshall the xml I had this error: Resource ServletContext resource [/xsd/MX_seev_031_001_05. xsd] does not exist this is my application.properties:
context.path =xml.swift.spring.com
schema.location= xsd/MX_seev_031_001_05.xsd
this my bean of config:
@Bean
public Jaxb2Marshaller createJaxb2Marshaller(@Value("${context.path}") final String contextPath,
@Value("${schema.location}") final Resource schemaResource){
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath(contextPath);
marshaller.setSchema(schemaResource);
Map<String, Object> properties = new HashMap<>();
properties.put(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setMarshallerProperties(properties);
return marshaller;
the xsd file is under src/main/resources/xsd and this is my pom.xml:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.1</version>
<executions>
<execution>
<id>add-source-for-demoapp</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaDirectory>${project.basedir}/src/main/resources/xsd</schemaDirectory>
<schemaIncludes>
<include>*.xsd</include>
</schemaIncludes>
<!-- Other configuration options-->
</configuration>
</execution>
</executions>
what i'am missing?
thanks.
I had basically the same issue, when I started to use spring-boot-starter-data-rest in addition to spring-oxm (I also have spring-boot-starter-data-jpa) in my pom.xml.
The problem is with your 2nd auto injected argument; @Value("${schema.location}") final Resource schemaResource
So instead of
@Bean
public Jaxb2Marshaller createJaxb2Marshaller(@Value("${context.path}") final String contextPath, @Value("${schema.location}") final Resource schemaResource){
//...
marshaller.setSchema(schemaResource);
//...
}
Do below;
@Bean
public Jaxb2Marshaller createJaxb2Marshaller(@Value("${context.path}") final String contextPath, @Value("${schema.location}") final String schemaLocation){
//...
Resource schemaResource = new ClassPathResource(schemaLocation);
marshaller.setSchema(schemaResource);
//...
}
Give it a try, it will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With