问题描述
我试图让 Spring 中的控制器使用 Jackson 类作为 推荐使用 3.0.当然,我的类路径中有 jackson jar 文件(jackson-core-asl-1.5.5.jar & jackson-mapper-asl-1.5.5.jar).
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.
至于 appconfig.xml 条目,我不确定我是否需要这些.在返回 ol'fashion non-json ajax 之前,我已经把它们放在那里作为最后的绝望行为.
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.
在调试中,我看到控制器收到请求,返回 foo,然后在 firebug 中得到 406.
In debug, I watch the controller get the request, return the foo and then, in firebug, get a 406.
错误信息如下:设置为调试时从记录器:org.springframework.web.HttpMediaTypeNotAcceptableException:找不到可接受的表示
The error messages are as follows:From the logger when set to debug:org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
来自回复:(406) 此请求标识的资源只能根据请求接受"头()生成具有不可接受特征的响应.
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 ().
我的 appconfig.xml 在这里:
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>
我的控制器
@RequestMapping(value="foo/bar", method=RequestMethod.GET)
public @ResponseBody foo getFoo(@RequestParam String fooId) {
return new foo(fooId);
}
在进行ajax调用的jsp上:
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.
推荐答案
摆脱所有 Jackson bean 以及协商解析器中的 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.
这篇关于Spring 的 Json 没有得到适当的响应解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!