restTemplate接收400 500訊息體

2021-10-23 07:26:57 字數 1505 閱讀 1557

當服務端自定義400或500錯誤返回體時,使用resttemplate 請求接收不到訊息體。resttemplate 預設只會返回狀態碼為200的訊息體,需要重寫

resttemplate 內建了自己的處理異常的方法responseerrorhandler去處理異常

}當接收到client_errorserver_error時,直接拋異常

}解決辦法

重寫responseerrorhandler ,對響應的錯誤資訊不進行處理

}設定自定義resttemplate

@bean

public resttemplate myresttemplate()

注入resttemplate bean

@resource

(name =

"myresttemplate"

)private resttemplate resttemplate;

最後從返回的responseentity中取body 屬性,就可以取得服務端返回的訊息體了

string strobject = resttemplate.

postforobject

(url, requestentity, string.

class);

jsonobject jsonobject = json.

parseobject

(strobject)

;

RestTemplate使用總結

resttemplate的介紹就不說了,總的來說用這個物件可以很方便的模擬乙個http請求。talk is cheap,show me the code,使用的是springboot整合的工程所以以下涉及到的物件都是註解式的宣告和注入,一 宣告resttemplate物件 當然,可以利用註解 bea...

RestTemplate使用總結

resttemplate的介紹就不說了,總的來說用這個物件可以很方便的模擬乙個http請求。talk is cheap,show me the code,使用的是springboot整合的工程所以以下涉及到的物件都是註解式的宣告和注入,一 宣告resttemplate物件 當然,可以利用註解 bea...

使用restTemplate隨筆

使用resttemplate訪問其他介面時 採用post方式傳參 發現服務端接受不到引數 坑了半天 記錄一下 服務端是個servlet 以post接參 所以本地使用mvc模擬了一下 方便除錯 如下 客戶端 如下 test public void test1 不能以map或hashmap代替 否則服務...