天天看點

使用RestTemplate發送post請求

最近使用RestTemplate發送post請求,遇到了很多問題,如轉換httpMessage失敗、中文亂碼等,調了好久才找到下面較為簡便的方法:

如果直接使用在postForObject中把對象傳入很容易出現no suitable HttpMessageConverter found for request type的錯誤,建議直接先轉成字元串,見jsonObj.otString(),

網上有人說設定RestTemplate的HttpMessageConverter,試了一下要引入各種包。

另外要注意中文編碼問題,網上有人說StringHttpMessageConverter預設使用ISO-8859-1,要指定為UTF-8編碼,自己嘗試沒有成功,最後通過指定contentType的方式解決了。

http://liuxing.info/2015/05/21/RestTemplate實踐/

http://www.cnblogs.com/zemliu/archive/2013/07/28/3220517.html

https://github.com/JohnathanMarkSmith/springmvc-resttemplate-auth-test

https://github.com/JohnathanMarkSmith/springmvc-resttemplate-test