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

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

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

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

kwvwclae

kwvwclae1#

406不可接受
请求标识的资源只能根据请求中发送的accept头生成具有不可接受的内容特征的响应实体。
因此,您的请求接受头是application/json,而您的控制器无法返回该头。当找不到正确的httpmessageconverter以满足@responsebody注解的返回值时,就会发生这种情况。当您使用 <mvc:annotation-driven> ,给定类路径中的某些三维聚会库。
要么你的类路径中没有正确的jackson库,要么你没有使用 <mvc:annotation-driven> 指令。
我成功地复制了您的场景,使用这两个库和 headers="Accept=*/*" 指令。
jackson-core-asl-1.7.4.jar
jackson-mapper-asl-1.7.4.jar

o2gm4chl

o2gm4chl2#

我也有同样的问题,在最新的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”头不可接受

py49o6xq

py49o6xq3#

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

puruo6ea

puruo6ea4#

我有同样的问题,我的控制器方法执行,但响应是错误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 .

c0vxltue

c0vxltue5#

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

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

qoefvg9y6#

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

vwkv1x7d

vwkv1x7d7#

检查 <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中的这些依赖项

uxh89sit

uxh89sit8#

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

wf82jlnq

wf82jlnq9#

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-base</artifactId>
    <version>2.6.3</version>
</dependency>
t5fffqht

t5fffqht10#

可能没有人向下滚动到现在,但是上面的解决方案都没有为我修复它,而是使我所有的getter方法 public 做。
我把getter的可见性留给了package private;Jackson决定找不到他们就爆炸了(使用 @JsonAutoDetect(getterVisibility=NON_PRIVATE) 只是部分修复了。

31moq8wy

31moq8wy11#

我遇到了同样的问题,因为我缺少@enablewebmvc注解(我的所有spring配置都是基于注解的,与xml等价的是mvc:annotation-driven)

ih99xse1

ih99xse112#

在控制器中,响应主体注解不应该在返回类型上而不是方法上,如下所示:

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

我还将使用原始jquery.ajax函数,并确保contenttype和datatype设置正确。
另一方面,我发现spring对json的处理相当有问题。当我自己用字符串和gson来做这一切时,就容易多了。

yyyllmsg

yyyllmsg13#

正如@atott提到的。
如果您在pom.xml中添加了最新版本的jackson,并且使用spring 4.0或更新版本,请使用 @ResponseBody 关于你的行动方法和 @RequestMapping 配置为 produces="application/json;charset=utf-8" ,但是,您仍然得到了406(不可接受),我想您需要在mvc dispatcherservlet上下文配置中尝试:

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
</bean>

这就是我最终解决问题的方法。

hi3rlvi2

hi3rlvi214#

Spring4.3.10:我使用了下面的设置来解决这个问题。
步骤1:添加以下依赖项

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.6.7</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.7</version>
</dependency>
<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>

步骤2:在mvc dispatcherservlet上下文配置中添加以下内容:

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>

<bean id="contentNegotiationManager"
    class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false"/>
    <property name="favorParameter" value="true"/>
    <property name="ignoreAcceptHeader" value="false" />
</bean>

自Spring3.2以来,根据默认配置,favorpathextension设置为true,因此如果请求uri具有任何适当的扩展,如 .htm spring将优先考虑延期。在步骤2中,我添加了contentnegotiationmanager bean来覆盖它。

guykilcj

guykilcj15#

确保在类路径中有正确的jackson版本

相关问题