Spring 的 Json 未通过适当的响应进行解析
我尝试让 Spring 中的控制器使用 Jackson 类返回 JSON 响应,但没有成功 推荐使用 3.0。当然,我的类路径中有 Jackson jar 文件(jackson-core-asl-1.5.5.jar & jackson-mapper-asl-1.5.5.jar)。
至于 appconfig.xml 条目,我不确定是否需要这些。在返回旧时尚非 json ajax 之前,我将它们放在那里作为最后的绝望行为。
在调试中,我观察控制器获取请求,返回 foo,然后在 firebug 中得到 406。
错误消息如下: 设置为调试时从记录器: org.springframework.web.HttpMediaTypeNotAcceptableException:无法找到可接受的表示
从响应中: (406)该请求所标识的资源仅能够生成具有根据请求“accept”标头()不可接受的特性的响应。
我的 appconfig.xml 在这里:
<!-- Configures support for @Controllers -->
<mvc:annotation-driven />
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<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/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"></property>
</bean>
我的控制器
@RequestMapping(value="foo/bar", method=RequestMethod.GET)
public @ResponseBody foo getFoo(@RequestParam String fooId) {
return new foo(fooId);
}
在 jsp 上,其中进行 ajax 调用:
function addRow() {
$.getJSON("foo/bar", {
fooId: 1
}, function(data) {
alert("it worked.");
});
}
让我知道是否需要更多信息。
I've tried to have a controller in Spring return a JSON response to no avail using the Jackson classes as recommended with 3.0. I've got the jackson jar files(jackson-core-asl-1.5.5.jar & jackson-mapper-asl-1.5.5.jar) in my class path of course.
As for the appconfig.xml entries, I'm not sure I need these. I've put them in there as a last act of desperation before returning to ol' fashion non-json ajax.
In debug, I watch the controller get the request, return the foo and then, in firebug, get a 406.
The error messages are as follows:
From the logger when set to debug:
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
From the response:
(406) The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().
My appconfig.xml is here:
<!-- Configures support for @Controllers -->
<mvc:annotation-driven />
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<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/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</list>
</property>
<property name="defaultViews">
<list>
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages"></property>
</bean>
My controller
@RequestMapping(value="foo/bar", method=RequestMethod.GET)
public @ResponseBody foo getFoo(@RequestParam String fooId) {
return new foo(fooId);
}
On the jsp, where the ajax call is made:
function addRow() {
$.getJSON("foo/bar", {
fooId: 1
}, function(data) {
alert("it worked.");
});
}
Let me know if there's any more info that is needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
摆脱所有 Jackson beans,以及协商解析器中的 json 映射。
mvc:annotation-driven
应该配置 Jackson 序列化工作所需的一切。Get rid of all Jackson beans, and of the json mapping in the negotiating resolver. the
mvc:annotation-driven
should configure everything you need for the Jackson serialization to work.我现在已经两次遇到这个错误了。
刚才我向我的 pojo 添加了 getter。 406 错误消失了,我得到了预期的 JSON。
我认为因为我的字段是受包保护的(默认访问权限),所以它会抓住它们,但我想不会。
作为记录,以防万一,我还使 POJO 实现了 Serialized、toString()、serialVersionUID、无参数构造函数和显式构造函数。
上次我清理/清除/刷新了我的 Tomcat 缓存,并做了其他任何事情来强制它重新加载。我相信当我添加 Jackson 依赖项时,它修复了我的编译时错误,但由于 tomcat 错过了它们,所以在运行时 Spring MVC 没有发现 Jackson 库,并产生了关于不可接受的响应类型的 406 错误。
I've had this error twice now.
Just now I added getters to my pojo. The 406 error went away and I got JSON as expected.
I assume that because my fields were package-protected (the default access), it would grab them, but I guess not.
For the record, in case it matters, I also made the POJO implement Serializable, toString(), serialVersionUID, no-arg constructor, and explicit constructors.
The prior time I cleaned/cleared/refreshed my Tomcat cache and did whatever else to force it to reload. I believe when I added the Jackson dependencies, it fixed my compile time errors, but since tomcat missed them, at runtime Spring MVC did not discover the Jackson libraries, and produced the 406 error about unacceptable response type.
另外,请确保添加两个 Jackson 相关的 jar 文件。
jackson-core-asl-1.9.8.jar
jackson-mapper-asl-1.9.8.jar
版本可以不同。
Also, make sure that you add two jackson related jar files.
jackson-core-asl-1.9.8.jar
jackson-mapper-asl-1.9.8.jar
The version can be different.
我知道这是一个旧线程,但也许有人会遇到和我一样的问题。将应用程序从 Spring 4.0.3.RELEASE 迁移到 Spring 4.1.0.RELEASE 时,我遇到了该异常。就我而言,将 Jackson 从 1.9.x 更新到 2.4.x 就成功了。
I know that this is an old thread, but maybe someone will encounter the same problem as me. I got that exception when migrating an application from Spring 4.0.3.RELEASE to Spring 4.1.0.RELEASE. In my case updating Jackson from 1.9.x to 2.4.x did the trick.
从 spring 3.x 迁移到 spring 4.x 时,我遇到了同样的异常。
我通过将 Jackson 依赖项从 更新为 解决了这个问题,
我
不需要其他任何东西。
I have got same exception when migrating from spring 3.x to spring 4.x.
I solved it with updating jackson dependencies from
to
Nothing else was needed for me.
发送 Accept: application/xml 标头不起作用。但是发送 Accept: application/json 确实有效,并且 jackson 映射器启动。我摆脱了 406 并以 json 格式获取了序列化的 java 对象,没有比 @ResponseBody 更多的配置并返回 new MyObject()
:)
感谢 skaffman 提供此信息,并感谢 Bozho 提供工作标头值 :D
Sending the Accept: application/xml header does not work. But sending Accept: application/json actually works, and jackson mapper kicks in. I got rid of my 406 and got my serialized java object in json format with no more config than @ResponseBody and return new MyObject()
:)
Thanks skaffman for this information, and thanks Bozho for the working header value :D
这个问题在网上随处可见,我被它咬了好几次。 Spring 3.0.6(可能还有 5)在渲染 json 时存在一些问题。一旦我更改为 3.1.0.RELEASE 版本,一切都会按原样运行。无需任何配置更改。需要注意的是,返回方法必须具有@ResponseBody(如前面的示例所示)并且必须位于 servlet-context.xml 或您的 spring 上下文配置文件中。
This question pops up everywhere on the net and I got bitten by it a couple of times. Spring 3.0.6 (and 5 possibly) has some issues in rendering json. Once I changed to 3.1.0.RELEASE version eveyrything worked AS IS. Without any config changes. Things to note is, the return method must have @ResponseBody (as in the example before) and must be in servlet-context.xml or your spring context configuration file.
可能我的回答有点晚了,但它可能会帮助其他人访问这个问题。
我通过添加解决了我的问题
,并给了我另一个异常(类未找到异常:org.slf4j.LoggerFactory),我通过添加解决了这个异常,
我希望它会对某人有所帮助。
May be my answer is a bit late, but it may help some one else visiting this question.
I got my problem resolved by adding
and gave me another exception (class not found exception: org.slf4j.LoggerFactory) which i resolved by adding
I hope it will help someone.