这是我的javascript:
function getWeather() {
$.getJSON('getTemperature/' + $('.data option:selected').val(), null, function(data) {
alert('Success');
});
}
这是我的 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 />
收到此错误:
GET http://localhost:8080/web/getTemperature/2 406 (Not Acceptable)
标题:
响应 header
Server Apache-Coyote/1.1
Content-Type text/html;charset=utf-8
Content-Length 1070
Date Sun, 18 Sep 2011 17:00:35 GMT
请求 header
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 错误,但 hibernate 查询同时工作。 每次我在 Dropbox 中更改选择时,tomcat 日志都是这样说的:
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 中有两个类似的问题,我在那里尝试了所有接受的提示,但我猜它们没有用......
有什么建议吗?欢迎提问...
最佳答案
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.
因此,您的请求接受 header 是 application/json 并且您的 Controller 无法返回它。当找不到正确的 HTTPMessageConverter 来满足 @ResponseBody 注释的返回值时,就会发生这种情况。 HTTPMessageConverter 会在您使用 <mvc:annotation-driven>
时自动注册。 ,给定类路径中的某些 3-d 方库。
要么你的类路径中没有正确的 Jackson 库,要么你没有使用
<mvc:annotation-driven>
指令。
我成功复制了您的场景,并且使用这两个库运行良好,没有 headers="Accept=*/*"
指令。
- jackson-core-asl-1.7.4.jar
- jackson-mapper-asl-1.7.4.jar
关于java - Spring JSON 请求得到 406( Not Acceptable ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7462202/