Ресурс Ресурс ServletContext не существует

Я работаю над проектом (весенняя загрузка), и мне нужно преобразовать XML-файл в классы Java с помощью плагина maven jaxb2. Я перехожу по этой ссылке: классы генерируются, проблема заключается в том, что когда я пытаюсь разобрать xml, у меня была эта ошибка: ресурс Resource ServletContext [/xsd/MX_sev_031_001_05. xsd] не существует, это мое приложение.Свойства:

context.path =xml.swift.spring.com
schema.location= xsd/MX_seev_031_001_05.xsd

это мой компонент конфигурации:

@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>

what i'am missing?

Спасибо.


person Cifer Ulquiorra    schedule 15.04.2017    source источник


Ответы (1)


У меня была в основном та же проблема, когда я начал использовать spring-boot-starter-data-rest в дополнение к spring-oxm (у меня также есть spring-boot-starter-data-jpa) в моем pom.xml.

Проблема заключается в вашем втором аргументе с автоматическим вводом; @Value("${schema.location}") final Resource 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);
    //...
}

Попробуйте, это сработает.

person Ilker P    schedule 02.06.2017