2014-09-27 6 views
4

http://www.ncpdp.org의 XSD 파일 (구성원 만 사용 가능)에서 많은 Java 파일을 생성 중입니다. 생성 후, Spring Controller에서 사용하고 싶지만 응답을 XML로 변환하는 데 문제가 있습니다.스프링 부트 컨트롤러에서 JAXB 생성 요소를 반환

JAXBElement < T>와 마찬가지로 요소 자체를 반환하려고 시도했지만 아무 것도 작동하지 않는 것 같습니다. 시험은 아래에 실패

java.lang.AssertionError: Status 
Expected :200 
Actual :406 

@Test 
public void testHelloWorld() throws Exception { 
    mockMvc.perform(get("/api/message") 
      .accept(MediaType.APPLICATION_XML) 
      .contentType(MediaType.APPLICATION_XML)) 
      .andExpect(status().isOk()) 
      .andExpect(content().contentType(MediaType.APPLICATION_XML)); 
} 

여기 내 컨트롤러 : 나는 봄 부팅의 MVC의 설정을 무시하는 MvcConfig을 만드는 시도했습니다

import org.ncpdp.schema.transport.MessageType; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

@RestController 
public class HelloWorldController { 

    @RequestMapping(value = "/api/message", method = RequestMethod.GET) 
    public MessageType messageType() { 
     return new MessageType(); 
    } 
} 

했지만, 제대로 동작하지 않습니다.

@Configuration 
@EnableWebMvc 
public class MvcConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { 
     converters.add(marshallingHttpMessageConverter()); 
    } 

    @Bean 
    public MarshallingHttpMessageConverter marshallingHttpMessageConverter() { 
     Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller(); 
     jaxb2Marshaller.setPackagesToScan(new String[]{"com.ncpdb.schema.transport"}); 

     MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter(); 
     converter.setMarshaller(jaxb2Marshaller); 
     converter.setUnmarshaller(jaxb2Marshaller); 
     converter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_XML)); 

     return converter; 
    } 
} 

생성 된 JAXB 객체를 XML로 마샬링하기 위해 Spring MVC를 얻으려면 어떻게해야합니까?

+0

질문 : 생성 된'MessageType'은 그것에'@XmlRootElement' 주석을 가지고 있습니까? 기본 XML 변환기 인'Jaxb2RootElementHttpMessageConverter'가이 주석을 사용하여 마샬링을 지원할 MessageType의 인스턴스를 고려해야합니다. –

+0

@BijuKunjummen이 맞습니다! https://github.com/spring-projects/spring-boot/issues/407 – geoand

+0

GET에 실제로 content-type application/xml이 있습니까? 어쩌면 변환 할 수없는 요청일까요? –

답변

3

필자는 내 스키마와 동일한 디렉토리에 bindings.xjb 파일을 만들어이 문제를 해결할 수있었습니다. 이것에 의해, JAXB는 클래스상에서 @XmlRootElement를 생성합니다.

<?xml version="1.0"?> 
<jxb:bindings version="1.0" 
       xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
       xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
       xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"> 

    <jxb:bindings schemaLocation="transport.xsd" node="/xsd:schema"> 
     <jxb:globalBindings> 
      <xjc:simple/> 
     </jxb:globalBindings> 
    </jxb:bindings> 
</jxb:bindings> 

나는 몇 인수를 추가 받는다는 - JAXB2-플러그인을 수정했다, 반환 된 XML에 네임 스페이스 접두사를 추가합니다.

<arg>-extension</arg> 
<arg>-Xnamespace-prefix</arg> 

그리고 종속성 추가 :

<dependencies> 
    <dependency> 
     <groupId>org.jvnet.jaxb2_commons</groupId> 
     <artifactId>jaxb2-namespace-prefix</artifactId> 
     <version>1.1</version> 
    </dependency> 
</dependencies> 

다음이 포함 내 bindings.xjb을 수정

<?xml version="1.0"?> 
<jxb:bindings version="1.0" 
       xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
       xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
       xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:namespace="http://jaxb2-commons.dev.java.net/namespace-prefix" 
       xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd 
       http://jaxb2-commons.dev.java.net/namespace-prefix http://java.net/projects/jaxb2-commons/sources/svn/content/namespace-prefix/trunk/src/main/resources/prefix-namespace-schema.xsd"> 

    <jxb:bindings schemaLocation="transport.xsd" node="/xsd:schema"> 
     <jxb:globalBindings> 
      <xjc:simple/> 
     </jxb:globalBindings> 

     <jxb:schemaBindings> 
      <jxb:package name="org.ncpdp.schema.transport"/> 
     </jxb:schemaBindings> 
     <jxb:bindings> 
      <namespace:prefix name="transport"/> 
     </jxb:bindings> 
    </jxb:bindings> 
</jxb:bindings> 

내가 https://java.net/projects/jaxb2-commons/pages/Namespace-prefix에서이 작업을 수행하는 방법을 학습합니다. 나는 또한 http://blog.frankel.ch/customize-your-jaxb-bindings이 JAXB 바인딩을 커스터마이징하는 방법에 대한 좋은 자료라는 것을 알았다.

관련 문제