从 Spring 3 迁移到 Spring 4.1 带有 Ajax/JSON 请求的 HTTP 状态 406

Migration from Spring 3 to Spring 4.1 HTTP Status 406 with Ajax/JSON request

我已将我的应用程序从 Spring 3 迁移到 Spring 4.1.7。 现在,当我执行 ajax 查询时,服务器响应 HTTP 406 错误。

POST /extranet/EmailIdentificationGetFicheClient.html HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
Accept: application/json, */*; q=0.01
Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8

回复:

HTTP/1.1 406 Inacceptable Server: Apache-Coyote/1.1 Content-Type: text/html;charset=utf-8 Content-Language: fr Content-Length: 1110 Date: Wed, 26 Aug 2015 08:28:08 GMT

我在类路径中有最新的 jackson 库:

<dependency org="com.fasterxml.jackson.datatype" name="jackson-datatype-json-org" rev="2.6.1" />
<dependency org="org.codehaus.jackson" name="jackson-mapper-asl" rev="1.9.13" />

我的dispatcher-servlet.xml

<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
    <bean class = "org.springframework.http.converter.StringHttpMessageConverter">
           <property name="supportedMediaTypes" value="text/plain; charset=UTF-8" />
    </bean>    
    <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter">
            <property name="supportedMediaTypes" value="application/xml"/>
    </bean>
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes" value="application/json"/>
    </bean>

</mvc:message-converters>
</mvc:annotation-driven>

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
 <property name="favorPathExtension" value="false" />
 <property name="useJaf" value="false" />
 <property name="useNotAcceptableStatusCode" value="false" />
 <property name="viewResolvers">
    <list>
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
            <property name="order" value="1" />
        </bean>                     
    </list>
</property>
<property name="defaultViews">
    <list>
        <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
    </list>
</property>

由于应用程序与 struts 一起存在,spring servlet 被映射到 *.html URL。

<servlet>
 <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>*.html</url-pattern>
</servlet-mapping>

我们有几个 ajax 查询,例如这个:

$.ajax({
    type : "post",
    accepts : {
      json : 'application/json'
    },
    url : "/extranet/emailIdentification.html",
    cache : false,
    dataType : 'json', ...

控制器代码(我可以在调试中看到它正确地在那里)。

@RequestMapping(value="/extranet/EmailIdentification")
@ResponseBody
public Contact getContactFromEmail(HttpServletRequest request) throws SpringException {

此外,我们正在使用 spring-security,我们正在尝试做的是让 spring 像在以前的版本中那样识别接受 header。

感谢您的宝贵时间。

问题的原因是 Spring 无法加载 Jackson。默认情况下它不会被依赖项加载。添加依赖项后

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-jaxrs</artifactId>
    <version>1.9.2</version>
  </dependency>

在浏览器中输入地址后返回了 JSON,没有使用 Accept headers 的任何技巧(正如它应该做的那样)。

我终于找到问题所在了。

问题不是由我的 jackson 库引起的,因为它们已正确加载。

我在调试中注意到我收到了一个异常 HttpMediaTypeNotAcceptableException

此异常是由于 ContentNegotiationStrategy 加载的是使用路径扩展而不是 HTTP headers,尽管我将视图解析器配置为忽略路径扩展:

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="favorPathExtension" value="false" />
    <property name="useJaf" value="false" />
    <property name="useNotAcceptableStatusCode" value="false" />
    <property name="viewResolvers">

我还添加了一个内容管理器工厂:

<bean id="cnManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
    <property name="useJaf" value="false" />
    <property name="ignoreAcceptHeader" value="false" />
</bean>

但是这个工厂从未被考虑在内,相反,spring 加载了它自己的名为 mvcContentNegotiationManager 的版本。

因此,最后的破解是强制使用正确的 class,这是通过 mvc:annotation-driven 标签设置它来完成的:

<mvc:annotation-driven content-negotiation-manager="cnManager" />

Et Voilà。现在我仍然可以在 spring 4 中使用 .html 扩展名,即使是 json 查询。