Spring boot 资源ServletContext资源不存在
我正在从事一个项目(SpringBoot),我必须使用maven jaxb2插件将xml文件转换为Java类。我正在关注这个链接: 类是生成的。问题是,当我尝试解组xml时,我遇到了以下错误: 资源ServletContext资源[/xsd/MX_seev_031_001_05.xsd]不存在 这是我的应用程序。属性:Spring boot 资源ServletContext资源不存在,spring-boot,xsd,maven-jaxb2-plugin,Spring Boot,Xsd,Maven Jaxb2 Plugin,我正在从事一个项目(SpringBoot),我必须使用maven jaxb2插件将xml文件转换为Java类。我正在关注这个链接: 类是生成的。问题是,当我尝试解组xml时,我遇到了以下错误: 资源ServletContext资源[/xsd/MX_seev_031_001_05.xsd]不存在 这是我的应用程序。属性: context.path =xml.swift.spring.com schema.location= xsd/MX_seev_031_001_05.xsd 这是我的配置bea
context.path =xml.swift.spring.com
schema.location= xsd/MX_seev_031_001_05.xsd
这是我的配置bean:
@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;
@Bean
公共Jaxb2Marshaller创建Jaxb2Marshaller(@Value(${context.path}”)最终字符串contextPath,
@值(“${schema.location}”)最终资源schemaResource){
Jaxb2Marshaller-marshaller=新的Jaxb2Marshaller();
setContextPath(contextPath);
marshaller.setSchema(schemaResource);
映射属性=新的HashMap();
put(Marshaller.JAXB_格式化的_输出,true);
setMarshallerProperties(属性);
返回编组员;
xsd文件位于src/main/resources/xsd下,这是我的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>
org.jvnet.jaxb2.maven2
maven-jaxb2-plugin
0.12.1
为demoapp添加源
生成
${project.basedir}/src/main/resources/xsd
*.xsd
我错过了什么
谢谢。当我开始在pom.xml中除了spring oxm之外使用spring boot starter数据rest(我也有spring boot starter数据jpa)时,我基本上遇到了相同的问题 问题在于你的第二个自动注入参数; @值(“${schema.location}”)最终资源schemaResource 所以不是
@Bean
public Jaxb2Marshaller createJaxb2Marshaller(@Value("${context.path}") final String contextPath, @Value("${schema.location}") final Resource schemaResource){
//...
marshaller.setSchema(schemaResource);
//...
}
做下面的事
@Bean
public Jaxb2Marshaller createJaxb2Marshaller(@Value("${context.path}") final String contextPath, @Value("${schema.location}") final String schemaLocation){
//...
Resource schemaResource = new ClassPathResource(schemaLocation);
marshaller.setSchema(schemaResource);
//...
}
试试看,它会有用的