springjson请求获取406(不可接受)

wpx232ag  于 2021-07-23  发布在  Java
关注(0)|答案(22)|浏览(294)

这是我的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查询同时工作。每当我在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=?

有什么问题吗?有两个类似的问题,所以之前,我尝试了所有接受的提示那里,但他们没有工作,我猜。。。
有什么建议吗?请随意提问。。。

uxh89sit

uxh89sit16#

确保发送的对象(在本例中是weather)包含getter/setter

vwkv1x7d

vwkv1x7d17#

检查 <mvc:annotation-driven /> 在dispatcherservlet.xml中,如果没有添加它。并添加

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

pom.xml中的这些依赖项

qoefvg9y

qoefvg9y18#

终于从这里找到了答案:
将restfulajax请求Map到spring
我引用:
@requestbody/@responsebody注解不使用普通的视图解析器,它们使用自己的httpmessageconverter。为了使用这些注解,您应该在annotationmethodhandleradapter中配置这些转换器,如参考中所述(您可能需要mappingjacksonhttpmessageconverter)。

c0vxltue

c0vxltue19#

确保以下2 jar 类路径中存在。
如果任何一个或两个都丢失了,那么这个错误就会出现。

jackson-core-asl-1.9.X.jar jackson-mapper-asl-1.9.X.jar
puruo6ea

puruo6ea20#

我有同样的问题,我的控制器方法执行,但响应是错误406。我调试 AbstractMessageConverterMethodProcessor#writeWithMessageConverters 找到了那个方法 ContentNegotiationManager#resolveMediaTypes 总是回来 text/html 不支持 MappingJacksonHttpMessageConverter . 问题是 org.springframework.web.accept.ServletPathExtensionContentNegotiationStrategy 工作时间早于 org.springframework.web.accept.HeaderContentNegotiationStrategy ,以及我请求的延期 /get-clients.html 是导致我的错误406的原因。我刚把请求url改为 /get-clients .

py49o6xq

py49o6xq21#

在另一种情况下,将返回此状态:如果jacksonMap程序无法确定如何序列化bean。例如,如果对同一布尔属性有两个访问器方法, isFoo() 以及 getFoo() .
所发生的事情是spring的mappingjackson2httpmessageconverter调用jackson的stdserializerprovider来查看它是否可以转换您的对象。在呼叫链的底部, StdSerializerProvider._createAndCacheUntypedSerializer 抛出 JsonMappingException 带着信息性的信息。然而,这个例外被 StdSerializerProvider._createAndCacheUntypedSerializer ,它告诉spring它不能转换对象。在用完转换器后,spring报告说它没有被赋予 Accept 它可以使用的标题,这当然是假的,当你给它 */* .
这个行为有一个bug,但是它被关闭为“无法复制”:被调用的方法没有声明它可以抛出,所以吞下异常显然是一个合适的解决方案(是的,这是讽刺)。不幸的是,Jackson没有任何日志记录。。。代码库中有很多评论希望它这样做,所以我怀疑这不是唯一隐藏的问题。

o2gm4chl

o2gm4chl22#

我也有同样的问题,在最新的Spring4.1.1之后,您需要向pom.xml添加以下jar。

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.1.1</version>
</dependency>

还要确保您有以下jar:

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

406 spring mvc json,根据请求“accept”头不可接受

相关问题