2015-04-02 116 views
0

我有一個REST API接受在POST一個字符串,返回一個對象,POST字符串中獲取400錯誤的請求Spring MVC中

這是方法:

@RequestMapping(method = RequestMethod.POST, value = "/aValue", headers = "Accept=application/json") 
public @ResponseBody 
MyObject getMyObject(@RequestBody String string) { 

    MyObject response = myService.getMyObject(string); 
    return response; 
} 

現在,當我打電話例如從另一個服務的API,如果我這樣做POST這樣它給了我總是400錯誤的請求:

List<Object> providers = new ArrayList<Object>(); 
    providers.add(jsonProvider); 

    WebClient client = WebClient.create(baseUrl + myAPI, providers); 

    client.type(MediaType.APPLICATION_JSON); 
    client.accept(MediaType.APPLICATION_JSON); 

    MyObject response = client.post(userId, MyObject.class); 

    return response; 

,而不是我用它工作的解決方案,這是一個:

MyObject response = client.post("\"" + userId + "\"", MyObject.class); 

有人可以幫我嗎?謝謝你們

回答

0

你有一個問題,因爲你發佈的內容不是有效的JSON,但你指出它在你的客戶端代碼。你似乎通過只是一個簡單的字符串屬性用戶id你可以簡單地改變你的映射加入consumes = "text/plain"接受純文本,

@RequestMapping(method = RequestMethod.POST, value = "/aValue", headers = "Accept=application/json", consumes = "text/plain") 
public @ResponseBody 
MyObject getMyObject(@RequestBody String string) { 

,讓您的客戶端發送純文本,所以

client.type(MediaType.TEXT_PLAIN); 
+0

消耗是不可用的,因爲我們使用的是spring 3.0.7.RELEASE ...並且據我所知我們cannpot升級版本 – alessioarsuffi 2015-04-02 09:04:26

+0

嘗試省略它,正確設置的內容類型應該足夠 – 2015-04-02 09:05:58