小编典典

Spring JSON请求获取406(不可接受)

spring

这是我的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

<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错误,但hibernate查询同时起作用。 每当我在保管箱中更改选择时,这就是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中有两个类似的问题,我在那里尝试了所有被接受的提示,但是我想它们没有用…

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


阅读 565

收藏
2020-04-11

共1个答案

小编典典

406 Not Acceptable

由请求标识的资源仅能够生成响应实体,该响应实体具有根据请求中发送的接受标头不可接受的内容特征。

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

你的类路径中没有正确的Jackson库,或者你没有使用 <mvc:annotation-driven>指令。

我成功地复制了你的方案,并且使用这两个库并且没有headers="Accept=*/*"指令,它可以正常工作。

  • jackson-core-asl-1.7.4.jar
  • jackson-mapper-asl-1.7.4.jar
2020-04-11