春天JSON请求获得406(不接受) [英] Spring JSON request getting 406 (not Acceptable)

查看:267
本文介绍了春天JSON请求获得406(不接受)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的javascript:

 函数的GetWeather(){
        $ .getJSON('getTemperature /+ $('。数据选项:选择)VAL(),空,功能(数据){。
            警报(成功);
        });
    }
 

这是我的控制器:

  @RequestMapping(值=/ getTemperature / {ID},标题=接受= * / *,方法= RequestMethod.GET)
@ResponseBody
公共气象getTemparature(@PathVariable(ID)整数ID){
    天气天气预报= weatherService.getCurrentWeather(ID);
        返回天气;
}
 

弹簧servlet.xml中

 <背景:注解配置/>
< TX:注解驱动/>
 

收到此错误:

  GET的http://本地主机:8080 /网络/ getTemperature / 2 406(不接受)
 

标题:

响应头

 服务器Apache的狼/ 1.1
内容类型text / html;字符集= UTF-8
内容长度1070
日期星期日,2011 17时00分35秒格林尼治标准​​时间09月18日
 

请求头

 主机本地主机:8080
用户代理的Mozilla / 5.0(Windows NT的6.1; WOW64; RV:6.0.2)的Gecko / 20100101 Firefox的/ 6.0.2
接受应用程序/ JSON,文字/ javascript中,* / *; Q = 0.01
接受语言EN-US,EN; Q = 0.5
接受编码gzip压缩,紧缩
接收字符集ISO-8859-1,UTF-8,Q = 0.7,*; Q = 0.7
连接保持
的X请求-随着XMLHtt prequest
Referer的HTTP://本地主机:8080 /网络/天气
Cookie的JSESSIONID = 7D27FAC18050ED84B58DAFB0A51CB7E4
 

有趣,注意:

我得到406错误,但Hibernate查询工作的同时。 这是tomcat的日志说,每次当我改变选择在Dropbox的:

 选择weather0_.ID为ID0_0_,不受天气weather0_那里weather0_.ID = weather0_.CITY_ID为CITY2_0_0_,weather0_.DATE为DATE0_0_,weather0_.TEMP作为TEMP0_0_?
 

还有什么问题呢?有两个类似的问题之前,我尝试了所有的接受暗示在那里,但他们没有工作,我猜...

有什么建议?随意问的问题...

解决方案
  

406不可接受

     

该请求所标识的资源只能生成具有根据请求发送的接受报头不能接受的内容特性响应实体。

那么,你的要求接受头是应用程序/ JSON和控制器无法返回。这发生在正确的HTTPMessageConverter无法找到满足@ResponseBody注明返回值。当您使用HTTPMessageConverter自动注册的< MVC:注解驱动> ,在classpath中给予一定的三维党库

要么你没有正确的杰克逊库在类路径中,或者没有使用 < MVC:注解驱动> 指令

我成功地复制您的方案,它能正常工作使用这两个库,没有标题=接受= * / *指令。

  • 杰克逊核心ASL-1.7.4.jar
  • 杰克逊映射器-ASL-1.7.4.jar

this is my javascript:

    function getWeather() {
        $.getJSON('getTemperature/' + $('.data option:selected').val(), null, function(data) {
            alert('Success');                               
        });
    }

this is my controller:

@RequestMapping(value="/getTemperature/{id}", headers="Accept=*/*", method = RequestMethod.GET)
@ResponseBody
public Weather getTemparature(@PathVariable("id") Integer id){
    Weather weather = weatherService.getCurrentWeather(id);
        return weather;
}

spring-servlet.xml

<context:annotation-config />
<tx:annotation-driven />

Getting this error:

GET http://localhost:8080/web/getTemperature/2 406 (Not Acceptable)

Headers:

Response Headers

Server  Apache-Coyote/1.1
Content-Type    text/html;charset=utf-8
Content-Length  1070
Date    Sun, 18 Sep 2011 17:00:35 GMT

Request Headers

Host    localhost:8080
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept  application/json, text/javascript, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection  keep-alive
X-Requested-With    XMLHttpRequest
Referer http://localhost:8080/web/weather
Cookie  JSESSIONID=7D27FAC18050ED84B58DAFB0A51CB7E4

Interesting note:

I get 406 error, but the hibernate query works meanwhile. This is what tomcat log says, everytime when I change selection in dropbox:

 select weather0_.ID as ID0_0_, weather0_.CITY_ID as CITY2_0_0_, weather0_.DATE as DATE0_0_, weather0_.TEMP as TEMP0_0_ from WEATHER weather0_ where weather0_.ID=?

What could the problem be? There were two similar questions in SO before, I tried all the accepted hints there, but they did not work I guess...

Any suggestions? Feel free to ask questions...

解决方案

406 Not Acceptable

The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request.

So, your request accept header is application/json and your controller is not able to return that. This happens when the correct HTTPMessageConverter can not be found to satisfy the @ResponseBody annotated return value. HTTPMessageConverter are automatically registered when you use the <mvc:annotation-driven>, given certain 3-d party libraries in the classpath.

Either you don't have the correct Jackson library in your classpath, or you haven't used the <mvc:annotation-driven> directive.

I successfully replicated your scenario and it worked fine using these two libraries and no headers="Accept=*/*" directive.

  • jackson-core-asl-1.7.4.jar
  • jackson-mapper-asl-1.7.4.jar

这篇关于春天JSON请求获得406(不接受)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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