资源ServletContext资源不存在

时间:2017-04-15 18:07:17

标签: spring-boot xsd maven-jaxb2-plugin

我正在处理一个项目(spring boot),我必须使用maven jaxb2插件将xml文件转换为Java类。我关注此链接: 生成类的问题是当我尝试解组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>

我错过了什么?

感谢。

1 个答案:

答案 0 :(得分:2)

当我开始在我的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);
    //...
}

在下面做;

@Bean
public Jaxb2Marshaller createJaxb2Marshaller(@Value("${context.path}") final String contextPath, @Value("${schema.location}") final String schemaLocation){
    //...
    Resource schemaResource = new ClassPathResource(schemaLocation);
    marshaller.setSchema(schemaResource);
    //...
}

尝试一下,它会起作用。

相关问题