3 回答

TA貢獻1893條經驗 獲得超10個贊
您想要創建一個實現的類,ResponseErrorHandler然后使用它的一個實例來設置其余模板的錯誤處理:
public class MyErrorHandler implements ResponseErrorHandler {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
// your error handling here
}
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
...
}
}
[...]
public static void main(String args[]) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new MyErrorHandler());
}
此外,Spring還提供了一個類DefaultResponseErrorHandler,您可以擴展該類而不是實現接口,以防萬一您只想覆蓋該handleError方法。
public class MyErrorHandler extends DefaultResponseErrorHandler {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
// your error handling here
}
}
查看其源代碼,以了解Spring如何處理HTTP錯誤。
- 3 回答
- 0 關注
- 3674 瀏覽
添加回答
舉報