2013-12-12 37 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>