2013-12-12 38 views
-2

服務器正在返回406 XML代碼時的HTTP代碼,但在JSON的情況下正常工作。以下是代碼段和連接日誌,以及:Spring + BlazeDS + REST XML響應

庫:

BlazeDS的4.0,3.2春天,JBoss的7.1.1Final,Maven的3.0

REST MVC控制器:

@Controller 
@RequestMapping("/contacts") 
public class ContactsController { 

    @RequestMapping(method = RequestMethod.GET,produces= {"application/xml","application/json"}) 
    @ResponseStatus(value=HttpStatus.OK) 
    public @ResponseBody List<Contact> find(@RequestParam(required = false) String searchStr) { 

配置彈簧:

@Configuration 
@EnableWebMvc 
public class Config extends WebMvcConfigurerAdapter { 

    @Override 
    public void configureContentNegotiation(
      ContentNegotiationConfigurer configurer) { 
     configurer.defaultContentType(MediaType.APPLICATION_XML) 
       .mediaType("xml", MediaType.APPLICATION_XML) 
       .mediaType("json", MediaType.APPLICATION_JSON); 

     } 
    } 

Spring配置

<context:component-scan base-package="com.heksa.services" 
    use-default-filters="false"> 
    <context:include-filter expression="org.springframework.stereotype.Controller" 
     type="annotation" /> 
</context:component-scan> 
<bean id="contentNegotiationManager" 
    class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> 
    <property name="defaultContentType" value="application/xml" /> 

    <property name="mediaTypes"> 
     <map> 
      <entry key="json" value="application/json" /> 
      <entry key="xml" value="application/xml" /> 
     </map> 
    </property> 
</bean> 
<bean id="xmlViewer" 
    class="org.springframework.web.servlet.view.xml.MarshallingView"> 
    <constructor-arg> 
     <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
      <property name="classesToBeBound"> 
       <list> 
        <value>com.heksa.bean.Contact</value> 
       </list> 
      </property> 
     </bean> 
    </constructor-arg> 
</bean> 
<mvc:annotation-driven 
    content-negotiation-manager="contentNegotiationManager" /> 

<mvc:default-servlet-handler /> 

<!-- Flex-specific Configuration --> 
<flex:message-broker mapping-order="1"> 
    <flex:mapping pattern="/messagebroker/*" /> 
    <flex:message-service 
     default-channels="my-streaming-amf,my-longpolling-amf,my-polling-amf" /> 
    <flex:secured /> 
</flex:message-broker> 

JBoss的日誌:

14:13:06,228 INFO [org.springframework.oxm.jaxb.Jaxb2Marshaller] (MSC service thread 1-8) Creating JAXBContext with classes to be bound [class com.heksa.bean.Contact] 
14:13:06,241 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/contacts],methods=[GET],params=[],headers=[],consumes=[],produces=[application/xml || application/json],custom=[]}" onto public java.util.List<com.heksa.bean.Contact> com.heksa.services.ContactsController.find(java.lang.String) 
14:13:06,241 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/contacts/{id}],methods=[DELETE],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void com.heksa.services.ContactsController.delete(int) 
14:13:06,242 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-8) Mapped "{[/contacts],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.heksa.bean.Contact com.heksa.services.ContactsController.create(com.heksa.bean.Contact) 
14:13:06,296 INFO [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping] (MSC service thread 1-8) Mapped URL path [/**] onto handler 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0' 
+3

請求中的客戶端接受標頭是什麼? – Pedantic

+0

標題都已正確配置。感謝您離開評論。現在已經修復了。 – Phani

回答

1

問題

問題描述(這將是很好,如果OP問一個任務而不僅僅是描述問題)是由客戶端提供的Accept頭與服務器提供的頭部Content-Type不匹配導致的內容協商之一。

可信和/或官方來源

HTTP規範本身(RFC 2616)既是可信和正式。

Section 14.1描述Accept頭:

如果沒有接受頭字段存在,則假定所述 客戶端接受所有媒體類型。如果Accept頭字段存在, ,並且如果服務器無法發送可接受的響應(根據 )到合併的Accept字段值,則服務器應該發送406 (不可接受)響應。

Section 14.17描述了Content-Type標題。

解決方案

由於應用程序配置application/xml是默認的內容類型返回,如果你想了解XML的響應則必須斷言,沒有Accept頭在HTTP請求中指定或斷言HTTP請求中包含的HTTP標頭

Accept: application/xml

斷言由服務器發送的HTTP響應包含HTTP頭

Content-type: application/xml; charset=utf-8

您可以使用流量嗅探器準確查看通過電線發送的內容。一個受歡迎的工具是Wireshark

+1

聽到聽到!美麗的答案 – flup

+0

如上所述添加HttpMessageConverter後,它工作正常。 – Phani

-1

在SpringConfiguration文件中添加了如下所示的HttpMessageConverters之後就可以工作了。

<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"> 
      <constructor-arg ref="jaxbMarshaller" /> 
      <property name="supportedMediaTypes" value="application/xml" /> 
</bean> 

<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> 
    <property name="classesToBeBound"> 
     <list> 
      <value>com.heksa.bean.Contact</value> 
     </list> 
    </property> 
</bean>