3 回答

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊
您想要?jiǎng)?chuàng)建一個(gè)實(shí)現(xiàn)的類,ResponseErrorHandler然后使用它的一個(gè)實(shí)例來(lái)設(shè)置其余模板的錯(cuò)誤處理:
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還提供了一個(gè)類DefaultResponseErrorHandler,您可以擴(kuò)展該類而不是實(shí)現(xiàn)接口,以防萬(wàn)一您只想覆蓋該handleError方法。
public class MyErrorHandler extends DefaultResponseErrorHandler {
@Override
public void handleError(ClientHttpResponse response) throws IOException {
// your error handling here
}
}
查看其源代碼,以了解Spring如何處理HTTP錯(cuò)誤。
- 3 回答
- 0 關(guān)注
- 3696 瀏覽
添加回答
舉報(bào)