亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jQuery、SpringMVC@RequestBody和JSON-使其協同工作

jQuery、SpringMVC@RequestBody和JSON-使其協同工作

神不在的星期二 2019-08-03 07:03:10
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/>

在您的Spring配置XML中。

我也建議你讀這篇博文。它幫了我很多。Springblog-Spring3.0中的Ajax簡化

最新情況:

剛剛檢查了我的工作代碼@RequestBody工作正常。我的配置中也有這個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>

也許很高興能看到Log4j是在說。它通常提供更多的信息,根據我的經驗@RequestBody如果請求的內容類型不是Application/JSON..您可以運行Fiddler 2來測試它,甚至MozillaLiveHTTP頭插件也可以提供幫助。




查看完整回答
反對 回復 2019-08-05
?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

除了這里的答案.。

如果您在客戶端使用jQuery,這對我來說是有效的:

Java:

@RequestMapping(value = "/ajax/search/sync") public String sync(@RequestBody Foo json) {

jQuery(需要包含DouglasCrocford的json2.js才能具有JSON.strgify函數):

$.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');
    }});




查看完整回答
反對 回復 2019-08-05
  • 3 回答
  • 0 關注
  • 454 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號