赞
踩
转载: https://blog.csdn.net/cc007cc009/article/details/110071142
将
- @Bean
- public RestTemplate restTemplate(RestTemplateBuilder builder) {
- RestTemplate restTemplate = builder.build();
- return restTemplate;
- }
改为
- @Bean
- public RestTemplate restTemplate(RestTemplateBuilder builder) {
- RestTemplate restTemplate = builder.build();
- restTemplate.setErrorHandler(new RtErrorHandler());
- return restTemplate;
- }
- package cn.linksign.supervise.exception;
-
- import org.springframework.http.HttpStatus;
- import org.springframework.http.client.ClientHttpResponse;
- import org.springframework.web.client.DefaultResponseErrorHandler;
-
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * 功能:捕获RestTemplate异常
- *
- */
- public class RtErrorHandler extends DefaultResponseErrorHandler {
-
- @Override
- public boolean hasError(ClientHttpResponse response) throws IOException {
- return super.hasError(response);
- }
-
- @Override
- public void handleError(ClientHttpResponse response) throws IOException {
- HttpStatus statusCode = HttpStatus.resolve(response.getRawStatusCode());
- List<HttpStatus> donotDeal = new ArrayList<>(); // 白名单
- donotDeal.add(HttpStatus.UNAUTHORIZED);
-
- if (!donotDeal.contains(statusCode)) { // 非白名单则处理
- super.handleError(response);
- }
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。