根据请求“接受",Spring 3.x JSON状态406“特征不可接受".标头()" [英] Spring 3.x JSON status 406 "characteristics not acceptable according to the request "accept" headers ()"

查看:76
本文介绍了根据请求“接受",Spring 3.x JSON状态406“特征不可接受".标头()"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用Spring 3.xJSON中得到我的响应时,我得到了406 error此请求所标识的资源仅能够生成根据request接受"具有不可接受特征的响应标头()."

Upon trying to get my response in JSON using Spring 3.x, I get the 406 error "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()."

这是我的环境

* Spring 3.2.0.RELEASE
* included jackson-mapper-asl-1.7.9.jar, jackson-core-asl-1.7.9.jar
* Tomcat 6.x
* mvc:annotation-driven in Spring configuration XML file

我的控制器:

@RequestMapping("/contest")
public class ContestController {

    @RequestMapping(value="{name}", headers="Accept=*/*", method = RequestMethod.GET)
    public @ResponseBody Contest getContestInJSON(@PathVariable String name) {
        Contest contest = new Contest();
        contest.setName(name);
        contest.setStaffName(new String("contestitem1"));

        return contest;
    }

}

我的Spring配置文件

My Spring Configuration file

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="com.contestframework.controllers" />

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">

<property name="mediaTypes">
    <map>
      <entry key="atom" value="application/atom+xml"/>
      <entry key="html" value="text/html"/>
      <entry key="json" value="application/json"/>
    </map>
 </property>

 <property name="viewResolvers">
 <list>
  <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
  </bean>
 </list>
 </property>

 <property name="defaultViews">
  <list>
   <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
  </list>
 </property>

 </bean>

<mvc:annotation-driven />

</beans>

在此之后,我可以使用以下命令访问控制器:

After this I just access the Controller using below:

http://domain/SpringWebProject/json/contest/abcd

并且我得到的响应是状态406:此请求标识的资源只能根据请求"accept"标头()生成特性不可接受的响应."

and the response I get is Status 406: "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ()."

我还尝试了另一种机制,方法是使用Javascript AJAX访问此机制,以确保我的请求标头具有application/JSON,但这导致了相同的Status 406结果

I also tried an alternate mechanism by access this using Javascript AJAX to make sure my request header has application/JSON but this led to the same Status 406 result

$.getJSON('contest/abcd', function(data) {
console.log(data) }

这是从浏览器捕获的我的请求头:

Here is my REQUEST HEADER captured from browser:

Request URL:http://localhost:8080/SpringWebProject/json/contest/abcd
Request Method:GET
Status Code:406 Not Acceptable

Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:JSESSIONID=59689C95B0B9C21494EB0AB9D9F7BCCD
Host:localhost:8080
Referer:http://localhost:8080/SpringWebProject/json/welcome
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4
X-Requested-With:XMLHttpRequest
Response Headersview source
Content-Length:1070
Content-Type:text/html;charset=utf-8
Date:Fri, 12 Oct 2012 18:23:40 GMT
Server:Apache-Coyote/1.1

感谢这方面的帮助.

推荐答案

您的配置没有错,但是我还是建议一些小的更改:

There is nothing wrong in your configuration, let me suggest a few small changes though:

a)您的命名空间似乎不正确-它们引用的是3.0模式,只需将其更改为3.1或不明确引用版本即可,例如.

a) Your namespaces appear wrong - they are referring to the 3.0 schemas, just change them to either 3.1 one's or don't refer to the version explicitly, this way for eg.

xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans.xsd

xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

b)您不需要ContentNegotiatingViewResolver,可以从配置中删除除component-scan<mvc:annotation-driven/>之外的所有内容

b) You don't require the ContentNegotiatingViewResolver, you can remove everything but the component-scan and <mvc:annotation-driven/> from your configuration

c)该请求将无法直接在浏览器中工作,因为它明确要求接受"application/json"的Accept标头-尽管发送正确的标头,但$.getJson调用应该可以工作

c) The request will not directly work from the browser as it explicitly requires an Accept header of "application/json" - $.getJson call should work though as it sends the correct headers

d)从@RequestMapping中删除headers=Acc..,并同时产生两个过滤条件,以匹配正确的映射方法调用.

d) Remove the headers=Acc.. from the @RequestMapping, and produces also, both are filtering criteria to match up the correct mapped method call.

有了这些,就没有理由不提供json了,您能尝试一下这些吗,看看它如何进行.

With these, there is no reason why the json should not get served out, can you please try with these and see how it goes.

这篇关于根据请求“接受",Spring 3.x JSON状态406“特征不可接受".标头()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆