Cif*_*rra 5 xsd maven-jaxb2-plugin spring-boot
我正在处理一个项目(spring boot),我必须使用 maven jaxb2 插件将 xml 文件转换为 Java 类。我正在关注此链接:生成类,问题是当我尝试解组 xml 时出现此错误:Resource ServletContext resource [/xsd/MX_seev_031_001_05. xsd] 不存在 这是我的 application.properties:
context.path =xml.swift.spring.com
schema.location= xsd/MX_seev_031_001_05.xsd
Run Code Online (Sandbox Code Playgroud)
这是我的配置 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;
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
谢谢。
小智 4
当我开始在 pom.xml 中使用 spring-boot-starter-data-rest 以及 spring-oxm (我也有 spring-boot-starter-data-jpa)时,我遇到了基本上相同的问题。
问题出在你的第二个自动注入的参数上;@Value("${schema.location}") 最终资源 schemaResource
所以而不是
@Bean
public Jaxb2Marshaller createJaxb2Marshaller(@Value("${context.path}") final String contextPath, @Value("${schema.location}") final Resource schemaResource){
//...
marshaller.setSchema(schemaResource);
//...
}
Run Code Online (Sandbox Code Playgroud)
执行以下操作;
@Bean
public Jaxb2Marshaller createJaxb2Marshaller(@Value("${context.path}") final String contextPath, @Value("${schema.location}") final String schemaLocation){
//...
Resource schemaResource = new ClassPathResource(schemaLocation);
marshaller.setSchema(schemaResource);
//...
}
Run Code Online (Sandbox Code Playgroud)
尝试一下,它会起作用的。
| 归档时间: |
|
| 查看次数: |
1283 次 |
| 最近记录: |