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 關注
- 454 瀏覽
添加回答
舉報
0/150
提交
取消