gpt4 book ai didi

Spring 3.x JSON 状态 406 "characteristics not acceptable according to the request "接受“headers ()”

转载 作者:IT老高 更新时间:2023-10-28 13:03:48 28 4
gpt4 key购买 nike

在尝试使用 Spring 3.xJSON 中获取我的响应时,我收到 406 错误“此请求标识的资源只能根据 request "accept"headers () 生成特征 Not Acceptable 响应。"

这是我的环境

* Spring 3.2.0.RELEASE
* included jackson-mapper-asl-1.7.9.jar, jackson-core-asl-1.7.9.jar
* Tomcat 6.x
* mvc:annotation-driven in Spring configuration XML file

我的 Controller :

@RequestMapping("/contest")
public class ContestController {

@RequestMapping(value="{name}", headers="Accept=*/*", method = RequestMethod.GET)
public @ResponseBody Contest getContestInJSON(@PathVariable String name) {
Contest contest = new Contest();
contest.setName(name);
contest.setStaffName(new String("contestitem1"));

return contest;
}

}

我的 Spring 配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<context:component-scan base-package="com.contestframework.controllers" />

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">

<property name="mediaTypes">
<map>
<entry key="atom" value="application/atom+xml"/>
<entry key="html" value="text/html"/>
<entry key="json" value="application/json"/>
</map>
</property>

<property name="viewResolvers">
<list>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>

<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>

</bean>

<mvc:annotation-driven />

</beans>

在此之后,我只需使用以下方式访问 Controller :

http://domain/SpringWebProject/json/contest/abcd

我得到的响应是状态 406:“此请求标识的资源只能生成具有根据请求“接受” header () Not Acceptable 特征的响应。”

我还尝试了一种替代机制,方法是使用 Javascript AJAX 访问它,以确保我的请求 header 具有 application/JSON 但这导致相同的 Status 406 结果

$.getJSON('contest/abcd', function(data) {
console.log(data) }

这是我从浏览器捕获的请求 header :

Request URL:http://localhost:8080/SpringWebProject/json/contest/abcd
Request Method:GET
Status Code:406 Not Acceptable

Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:JSESSIONID=59689C95B0B9C21494EB0AB9D9F7BCCD
Host:localhost:8080
Referer:http://localhost:8080/SpringWebProject/json/welcome
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4
X-Requested-With:XMLHttpRequest
Response Headersview source
Content-Length:1070
Content-Type:text/html;charset=utf-8
Date:Fri, 12 Oct 2012 18:23:40 GMT
Server:Apache-Coyote/1.1

感谢您在这方面的任何帮助。

最佳答案

我也遇到了同样的问题。看来这是最新的 3.2.0.RELEASE 的问题,因为我之前有 3.1.2.RELEASE 并且一切正常。更改为 3.2.0.RELEASE 后,它会中断。已经用 3.1.3.RELEASE 测试过,效果很好。所以现在我建议回滚到 3.1.3.RELEASE

编辑:感谢本网站上链接到以下位置的另一篇文章:http://static.springsource.org/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-config-content-negotiation

我现在已经通过根据请求路径的扩展禁用获取媒体类型来使其工作。这可以通过以下方式完成:

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"/>
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<!-- Turn off working out content type based on URL file extension, should fall back to looking at the Accept headers -->
<property name="favorPathExtension" value="false" />
</bean>

并为所有 xsd 架构位置指定版本 3.2。

这是使用以下 jackson jar :

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

关于Spring 3.x JSON 状态 406 "characteristics not acceptable according to the request "接受“headers ()”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12865093/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com