我想有一个双向的JSON到Java序列化
我正在成功地使用 the Java to JSON to JQuery path...(@ResponseBody
)例如
@RequestMapping(value={"/fooBar/{id}"}, method=RequestMethod.GET)
public @ResponseBody FooBar getFooBar(
@PathVariable String id,
HttpServletResponse response , ModelMap model) {
response.setContentType("application/json");
...
}
在JQuery中,我使用
$.getJSON('fooBar/1', function(data) {
//do something
});
这对很有效(例如,注释已经可以工作了,这要感谢所有的答复者)
但是,如何实现反向路径:是否使用RequestBody将JSON序列化为Java对象?
无论我怎么尝试,我都不能让这样的东西工作:
@RequestMapping(value={"/fooBar/save"}, method=RequestMethod.POST)
public String saveFooBar(@RequestBody FooBar fooBar,
HttpServletResponse response , ModelMap model) {
//This method is never called. (it does when I remove the RequestBody...)
}
我正确地配置了Jackson (它会在输出时序列化),当然,我还将MVC设置为注解驱动
我该如何让它工作呢?这是完全可能的吗?或者Spring / JSON / JQuery是单向的?
更新:
我改变了杰克逊的设置
<bean id="jsonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
<!-- Bind the return value of the Rest service to the ResponseBody. -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list id="beanList">
<ref bean="jsonHttpMessageConverter" />
<!-- <ref bean="xmlMessageConverter" /> -->
</util:list>
</property>
</bean>
对(几乎相似的)建议
<bean id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter" />
</list>
</property>
</bean>
而且它似乎起作用了!我不知道到底是什么成功了,但它确实有效...
https://stackoverflow.com/questions/5908466
复制相似问题