2010-03-08 7 views
6

xml 콘텐츠가있는 "게시물"을 지원하는 간단한 스프링 기반 웹 서비스를 만들려고합니다. 봄xml to Spring REST 서버가 반환하는 지원되지 않는 미디어 유형

, 나는 AnnotationMethodHandler을 정의

<bean id="inboundMessageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
     <property name="messageConverters"> 
      <util:list> 
       <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> 
        <property name="marshaller" ref="xmlMarshaller"/> 
        <property name="unmarshaller" ref="xmlMarshaller"/> 
       </bean> 
      </util:list> 
     </property> 
    </bean> 

그리고 JAXB 기반 XML 마샬 :

<bean id="xmlMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
     <property name="contextPaths"> 
      <array> 
       <value>com.company.schema</value> 
      </array> 
     </property> 
     <property name="schemas"> 
      <array> 
       <value>classpath:core.xsd</value> 
      </array> 
     </property> 
    </bean> 

내 컨트롤러는 "자원이"JAXB에 의해 자동 생성 클래스이고, 다음과 같이 주석이 :

@RequestMapping(method = POST, value = "/resource") 
    public Resource createResource(@RequestBody Resource resource) { 
     // do work 
    } 

웹 서비스 호출의 결과는 항상 "HTTP/1.1 415 지원되지 않음 Med ia 유형 ". 다음은 서비스 호출 예입니다.

HttpPost post = new HttpPost(uri); 
post.addHeader("Accept", "application/xml"); 
post.addHeader("Content-Type", "application/xml"); 

StringEntity entity = new StringEntity(request, "UTF-8"); 
entity.setContentType("application/xml"); 
post.setEntity(entity); 

가능한 모든 미디어 유형을 설정할 수 있습니다. 누구나 아이디어가 있습니까?

편집 : 추가 디버깅 후 객체를 비동기 해제하려고 시도하는 것처럼 보이지 않습니다. AnnotationMethodHandler가 application/xml 유형이 무언가 MarshallingHttpConverter에 가야한다는 것을 아는 뒤에있는 검은 마법을 이해하지 못합니다. 누구든지 그 빛을 비춰 줄 수 있습니까?

답변

5

가장 일반적인 이유는 JAXB 컨텍스트가 Resource 오브젝트로 비 정렬 화하는 방법을 알지 못하기 때문입니다.

Resource에는 @XMLRootElement 주석이 있습니까? 그렇지 않은 경우 Jaxb2Marshaller은 매개 변수를 허용하지 않으므로 415 오류가 발생합니다. 이것은 Sprng에서 JAXB 런타임으로의 위임을 통해 수행됩니다. Spring은이 문제에 관해 많은 것을 가지고 있지 않습니다.

편집 다음 @RequestBody 매개 변수가 HandlerMethodInvoker.resolveRequestBody()에서 수행에의 데이터의 실제 강제. MIME 유형과 매개 변수 클래스 유형의 일치를 포함하여 일치시키기 전에 충족되어야하는 많은 조건이 있으며, 실패하면 로깅이없고 HTTP 415 만 있습니다. 해당 메소드의 소스를 살펴보십시오 더 나은 방법은 원격 디버깅을 수행하여 논리 설정에 실패한 부분을 확인하는 것입니다.

+0

제안에 감사드립니다. skaffman. 나는 검사하고 클래스는 실제로 @XMLRootElement 태그를 가지고있다. –

+0

@Mayra :'Resource'는'com.company.schema' 패키지에 있습니까? config로'Jaxb2Marshaller'를 구성하는 단위 테스트를 작성하고'support (Resource.class)'가'true'를 리턴하는지 확인하십시오. 당신이 더 이상 얻을 수 있기 전에 그것은 일할 필요가 있습니다. – skaffman

+0

주사위가 없으므로 Jaxb2Marshaller 객체를 만들었고 springfile에있는 contextPath와 schema를 설정하고 클래스를 지원한다고 말했습니다. –

관련 문제