2013-12-09 24 views
2

我在Spring rest服務中有一個方法。具有整數值的請求參數的Spring RestRemplate postforobject

@RequestMapping(value = "test/process", method = RequestMethod.POST) 
public @ResponseBody MyResponse processRequest(String RequestId, int count) 

我正在使用Spring RestTemplate調用此服務。

RestTemplate restTemplate = this.getRestTemplate(); 
MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>(); 
map.add("RequestId", RequestId); 
map.add("count", count); 
restTemplate.postForObject(url, map,MyResponse.class); 

當我嘗試調用客戶端的方法我得到沒有合適的HttpMessageConverter發現請求類型[java.lang.Integer中]

org.springframework.http.converter.HttpMessageNotWritableException: Could not write request: no suitable HttpMessageConverter found for request type [java.lang.Integer] 
at org.springframework.http.converter.FormHttpMessageConverter.writePart(FormHttpMessageConverter.java:310) 
at org.springframework.http.converter.FormHttpMessageConverter.writeParts(FormHttpMessageConverter.java:270) 
at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:260) 
at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:200) 
at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:1) 
at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:596) 
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:444) 
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409) 
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:287) 

的例外,我知道的方法之一是以String形式傳遞所有參數。但我可能需要稍後將複雜的數據類型作爲參數傳遞。 什麼是實現這一目標的方法。 我google了一些選項似乎寫我自己的轉換器。我應該如何着手解決這個問題。

+0

在以下行中: map.add(「RequestId」,RequestId); 是RequestId比字符串更復雜的對象嗎? 我們可以看到網址嗎? 謝謝! – java1337

+0

@ java1337 RequestId當前只是一個字符串,但我可能需要在其他方法中發佈自定義數據類型或日期對象 – d123

回答

6

此錯誤的根本原因是,通過在LinkedMultiValueMap中指定IntegerRestTemplate將表示您的請求是多部分請求。默認情況下,沒有HttpMessageConverter可以處理將Integer類型的值寫入請求主體。

如您所說,您可以通過將count更改爲String來處理這種情況。畢竟,HTTP請求參數中沒有Integer類型。但是,你擔心

但我可能需要傳遞複雜的數據類型作爲參數後面。

假設是這樣的

public @ResponseBody MyResponse processRequest(String RequestId, int count, Complex complex) { 

public class Complex { 
    private String someValue; 
    private int intValue; 

    public String getSomeValue() { 
     return someValue; 
    } 

    public void setSomeValue(String someValue) { 
     this.someValue = someValue; 
    } 

    public int getIntValue() { 
     return intValue; 
    } 

    public void setIntValue(int intValue) { 
     this.intValue = intValue; 
    } 

    public String toString() { 
     return someValue + " " + intValue; 
    } 
} 

在下面的工作就好了

MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>(); 
map.add("RequestId", "asd"); 
map.add("count", "42"); 
map.add("someValue", "complex"); 
map.add("intValue", "69"); 
restTemplate.postForObject(url, map,MyResponse.class); 

記住,請求參數被用來填充字段模型屬性的名稱秒。

甚至更​​好的解決方案會讓你使用像JSON或XML這樣的序列化標準。

+0

感謝您的答覆。通過「使用像JSON或XML這樣的序列化標準」,你的意思是我應該創建一個RequestId,count和someValue作爲記錄的請求對象。併發布該對象。控制器應該有參數RequestBody MyClass myobject? – d123

+0

@aks不,我的意思是不發送請求參數,你應該發送JSON和XML。 Spring在處理程序方法參數上使用'@ RequestBody'註解支持這些類型。 –