资源 ServletContext 资源不存在
Resource ServletContext resource does not exist
我正在处理一个项目(spring 启动),我必须使用 maven jaxb2 插件将 xml 文件转换为 Java 类。我正在关注这个 link:
生成 类 问题是当我尝试解组 xml 时出现此错误:
资源 ServletContext 资源 [/xsd/MX_seev_031_001_05。 xsd]不存在
这是我的 application.properties:
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;
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>
我缺少什么?
谢谢。
当我开始使用 spring-boot-starter-data-rest 除了 spring-oxm(我还有 spring- boot-starter-data-jpa) 在我的 pom.xml.
问题出在你的第二个自动注入参数上;
@Value("${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);
//...
}
试一试,它会起作用。
我正在处理一个项目(spring 启动),我必须使用 maven jaxb2 插件将 xml 文件转换为 Java 类。我正在关注这个 link: 生成 类 问题是当我尝试解组 xml 时出现此错误: 资源 ServletContext 资源 [/xsd/MX_seev_031_001_05。 xsd]不存在 这是我的 application.properties:
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;
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>
我缺少什么?
谢谢。
当我开始使用 spring-boot-starter-data-rest 除了 spring-oxm(我还有 spring- boot-starter-data-jpa) 在我的 pom.xml.
问题出在你的第二个自动注入参数上; @Value("${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);
//...
}
试一试,它会起作用。