我正在尝试使用Spring 3.0.6返回JSON响应,但我得到406响应“Not Acceptable”,其描述如下:
“此请求标识的资源只能生成具有特征的响应
根据请求“接受”标题()不可接受.“
我知道之前曾经问过a very similar question,但是我不能让它为我的项目工作,尽管很多
测试,我不明白我做错了什么.
在我的Maven pom.xml中,我有以下内容:
org.codehaus.jackson
jackson-mapper-asl
1.8.5
compile
org.codehaus.jackson
jackson-core-asl
1.8.5
compile
在web.xml中,我引用webmvc-config.xml,日志确认已加载.
mainServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/spring/webmvc-config.xml
1
在webmvc-config.xml中我有以下内容:
我的控制器是:
@Controller
public class ClassifiedController {
@RequestMapping(value = "/classified/{idClassified}", headers = "Accept=*/*",
method = RequestMethod.GET)
@ResponseBody
public final Classified getClassified(@PathVariable final int idClassified) {
...
我尝试使用或不使用headers参数,但结果相同.如果我调用URL
直接使用Firefox,请求标头包含以下内容(使用firebug进行检查):
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
如果我使用以下JQuery:
$.ajax({
url: '/classified/38001',
type: 'GET',
dataType: 'json'
});
发送以下标头:
Accept: application/json, text/javascript, */*; q=0.01
在这两种情况下,结果都是406错误.我不知道还应该检查什么
这行得通.
更新:我决定通过Spring调试,我发现Jackson被正确调用,并且在org.codehaus.jackson.map.ser.StdSerializerProvider中,方法_findExplicitUntypedSerializer包含以下代码:
try {
return _createAndCacheUntypedSerializer(runtimeType, property);
} catch (Exception e) {
return null;
}
这是不幸的,因为隐藏了问题的根源.使用调试器,我发现该异常包含一个非常描述性的错误消息:
Conflicting getter definitions for property "reminded":
ClassifiedImpl#isReminded(0 params) vs
ClassifiedImpl#getReminded(0 params)
现在,我看到错误消息是一个愚蠢的错误,很容易修复,但没有它,这不是那么明显.事实上,修复问题,导致工作序列化.
解决方法:
标签:java,spring,json,spring-mvc,jackson
来源: https://codeday.me/bug/20190926/1818454.html