你想要创建一个实现的类,
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 }}


