2013-05-08 39 views
8

我試圖找出爲什麼我無法接收到jQuery.ajax調用的請求,然後Spring @Controller處理程序方法包含@RequestBody註釋。考慮以下幾點:Spring @MVC和@RequestBody註解與x-www-form-urlencoded數據?

HTML/JavaScript的

<form id="foo" action="/baz"> 
    <input name="bar"> 
</form> 

<script> 
    $(function() { 
    var $fooForm = $('#foo'); 

    $fooForm.on('submit', function(evt) { 
     evt.preventDefault(); 

     $.ajax({ 
     url: $fooForm.action, 
     data: $fooForm.serialize(), 
     dataType: 'json', 
     type: 'POST', 
     success: function(data) { console.log(data); } 
     }); 
    }); 
    }); 
</script> 

的Java

@RequestMapping(
    value = "/baz", 
    method = RequestMethod.POST, 
    consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, 
    produces = MediatType.APPLICATION_JSON_VALUE 
) 
public @ResponseBody SearchResults[] jqueryPostHandler(
    @RequestBody FormDataObject formData) 
{ 
    return this.searchService.find(formData); 
} 

以上將失敗,@RequestBody註釋本,並返回一個415錯誤(沒有異常會被生成)。但是,如果@RequestBody註釋被刪除(即參數簽名只是FormDataObject formData),那麼該方法將被調用,並且JSON將被返回給JavaScript。

爲什麼會出現這種情況?一個POST請求包含請求正文中的數據。不應該註釋處理這樣的請求嗎?

我意識到我可以將JavaScript發送的內容類型更改爲application/jsonconsumes屬性爲MediaType.APPLICATION_JSON_VALUE以使註釋正確工作。但爲什麼它不適用於正常的表單請求?

注意:我使用Spring 3.1.4。

+0

http://matthewsalvatore.blogspot.com.br/2013 /08/spring-framework-working-with-x-www.html – Allenaz 2016-12-27 19:31:23

回答

7

你有沒有試過打開'org.springframework.web'的日誌來找出返回的狀態碼的原因?在翻譯成415之前應該有異常提出並記錄。

此外,如果發送表單數據,爲什麼不忽略@RequestBody。然後,您將使用數據綁定(即@ModelAttribute)將Servlet請求參數應用於對象字段。這比使用FormHttpMessageConverter更可取。

+0

調用日誌記錄顯示,使用'@ RequestBody'註釋就可以將_does_映射到但它會拋出「org.springframework.web.HttpMediaTypeNotSupportedException:內容類型'application/x-www-form-urlencoded; charset = UTF-8'not supported」。 – 2013-05-10 14:50:58

+0

根據我的理解,註解'@ RequestBody'意味着要處理的數據將在請求的主體中。這會通知編譯器_and_讀取代碼的人。所以,讓它變得模糊的代碼。這就是爲什麼我想知道爲什麼這是失敗的。 – 2013-05-10 14:52:38

6

正如Spring Docs of @RequestBody所述,請求正文將由HttpMessageConverter轉換爲您的對象。

有4個缺省HttpMessageConverters:

要轉換url編碼形式,這是ajax.serialize()創建,這是FormHttpMessageConverter的工作。由於你有一個HttpMediaTypeNotSupportedException異常,我猜你沒有配置FormHttpMessageConverter。檢查你的Spring配置文件,這裏有一個例子:

< bean class ="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" > 
    < property name= "messageConverters" > 
     < list> 
      < ref bean= "mappingJacksonHttpMessageConverter" /> 
      < ref bean= "stringHttpMessageConverter" /> 
      <!-- Do you have this converter ? --> 
      < ref bean= "formHttpMessageConverter" /> 
     </ list> 
    </ property> 
</ bean> 
+0

即使我添加了所有轉換器,上述操作對我而言仍然不起作用。我必須使用http://stackoverflow.com/a/31083802/471213上的自定義轉換器才能使其工作 – 2015-09-10 09:42:19

+0

@ usr-local-ΕΨΗΕΛΩΝ與您的問題一樣嗎?我的回答是針對這個問題的,如果你的問題不同,或者你應該提出一個新問題來解釋更多細節。 – Qianyue 2015-09-10 09:50:01

+0

不完全一樣的確切問題:提交表單時提交JSON時出現錯誤406。但是,在我使用Spring的代碼進行調試之前,我不明白請求實體**必須**爲了與「FormHttpMessageConverter」一起工作而實現'MultiValueMap'。所以*現在*我開始認爲OP的'FormDataObject'實現了這個接口,否則我與OP有同樣的問題,但添加了這個轉換器不起作用。順便說一下,我使用的是Spring 4.2 – 2015-09-10 10:11:33

-2

的問題是,當我們使用應用程序/ x-WWW窗體-urlencoded,春不把它理解爲RequestBody。所以,如果我們想要使用這個 我們必須刪除@RequestBody註釋。

然後嘗試以下操作:

@RequestMapping(
     value = "/baz", 
     method = RequestMethod.POST, 
     consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, 
     produces = MediatType.APPLICATION_JSON_VALUE 
) 
public @ResponseBody SearchResults[] jqueryPostHandler(
     FormDataObject formData) 
{ 
    return this.searchService.find(formData); 
} 

注意,刪除註釋@RequestBody

答案Http Post request with content type application/x-www-form-urlencoded not working in Spring

+1

。他的問題是爲什麼。 – 2017-05-01 19:38:36