我试着让Spring中的控制器返回一个JSON响应,但无法使用Jackson类作为recommended with 3.0。我在课程路径中有杰克逊jar文件(jackson-core-asl-1.5.5.jar & jackson-mapper-asl-1.5.5.jar)。Spring的Json没有被正确的响应解决
至于appconfig.xml条目,我不知道我需要这些。我把它们放在那里作为绝望的最后一幕,然后回到ol时尚非json ajax。
在调试,我看控制器获得请求,返回FOO,然后在萤火,得到406
错误消息如下:设置调试 从记录器: 组织.springframework.web.HttpMediaTypeNotAcceptableException:无法找到可接受表示
从响应: (406)由该请求所标识的资源仅能够利用根据“接受”报头中的请求不被接受的特性产生响应的() 。
我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.");
});
}
让我知道是否有一个需要更多信息。
更多报道[春季JSON请求获取406(不可接受)](http://stackoverflow.com/questions/7462202/spring-json-request-getting-406-not-acceptable) – kdgregory 2012-08-16 01:45:49