jQuery、SpringMVC@RequestBody和JSON-使其协同工作我希望有一个指向Java序列化的双向JSON我在用成功Java到JSON到JQuery路径.。(@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});这行得通井(例如,由于所有的答案,注解已经起作用了)然而,我如何做倒转PATH:是否使用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是单向的(Out)?最新情况:我改变了杰克逊的背景<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>而且看起来很管用!我不知道这到底是怎么回事,但它起作用了.
3 回答
慕的地6264312
TA贡献1817条经验 获得超6个赞
<context:annotation-config/>
最新情况:
@RequestBody
<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>
Log4j
@RequestBody
Application/JSON
郎朗坤
TA贡献1921条经验 获得超9个赞
@RequestMapping(value = "/ajax/search/sync") public String sync(@RequestBody Foo json) {
$.ajax({ type: "post", url: "sync", //your valid url contentType: "application/json", //this is required for spring 3 - ajax to work (at least for me) data: JSON.stringify(jsonobject), //json object or array of json objects success: function(result) { //do nothing }, error: function(){ alert('failure'); }});
- 3 回答
- 0 关注
- 421 浏览
添加回答
举报
0/150
提交
取消