Spring JSON 请求获取 406(不可接受) [英] Spring JSON request getting 406 (not Acceptable)

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

问题描述

这是我的 javascript:

this is my javascript:

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

这是我的控制器:

@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

spring-servlet.xml

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

出现此错误:

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

标题:

响应头

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

请求标头

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

有趣的注释:

我收到 406 错误,但同时休眠查询有效.每次我在 dropbox 中更改选择时,tomcat log 就是这样说的:

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=?

可能是什么问题?之前在 SO 中有两个类似的问题,我尝试了那里所有接受的提示,但我猜它们没有用...

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 不可接受

请求标识的资源只能根据请求中发送的accept头生成具有不可接受的内容特征的响应实体.

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.

因此,您的请求接受标头是 application/json,而您的控制器无法返回该标头.当无法找到正确的 HTTPMessageConverter 来满足 @ResponseBody 注释的返回值时,就会发生这种情况.当您使用 <mvc:annotation-driven> 时,HTTPMessageConverter 会自动注册,给定类路径中的某些 3-d 方库.

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.

要么你的类路径中没有正确的 Jackson 库,要么你没有使用 指令.

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

我成功地复制了您的场景,并且使用这两个库运行良好,并且没有 headers=Accept=*/*" 指令.

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

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

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