RestTemplate capture 5xx, 4xx and other abnormalities

RestTemplate when the connection fails, it will throw an exception directly, we can set

restTemplate.setErrorHandler(new CustomErrorHandler());

To handle exceptions. But this is not enough flexibility, flexibility point can not occur in the business.

Another idea is to catch the exception in each case, according to the type of exception will know after the corresponding anomaly.
HttpClientErrorException - In the case of HTTP 4xx state of
HttpServerErrorException - in case of HTTP 5xx state of
UnknownHttpStatusCodeException - If the HTTP status is unknown
ResourceAccessException - Request timed out, Spring underlying thrown exception in this instance would be java.net.SocketTimeoutException.

Code

        @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/netTest")
    public String netTest() {
        try {
            //String url = "http://127.0.0.1:8080/statusCode3xx";
            //String url = "http://127.0.0.1:8080/statusCode4xx";
            //String url = "http://127.0.0.1:8080/statusCode5xx";
            String url = "http://127.0.0.1:8080/timeout";
            String result = restTemplate.getForObject(url, String.class);
            logger.info(" result = " + result);
        } catch (HttpClientErrorException e) {// 4xx
            logger.error(" HttpClientErrorException , e = " + e.getMessage());
            e.printStackTrace();
        } catch (HttpServerErrorException e) {// 5xx
            logger.error(" HttpServerErrorException , e = " + e.getMessage());
            e.printStackTrace();
        } catch (UnknownHttpStatusCodeException e) {
            logger.error(" UnknownHttpStatusCodeException , e = " + e.getMessage());
            e.printStackTrace();
        }catch(HttpStatusCodeException  e){
            logger.error(" HttpStatusCodeException , e = " + e.getMessage());
            e.printStackTrace();
        }catch(ResourceAccessException e){//timeout
            logger.error(" ResourceAccessException , e = " + e.getMessage());
            e.printStackTrace();
        }
        return "netOK";
    }

    @RequestMapping("/statusCode3xx")
    public ResponseEntity<String> returnStatusCode3XX() {
        return new ResponseEntity<String>(" error ", HttpStatus.USE_PROXY);
    }
    
    @RequestMapping("/statusCode4xx")
    public ResponseEntity<String> returnStatusCode4XX() {
        return new ResponseEntity<String>(" error ", HttpStatus.UNAUTHORIZED);
    }

    @RequestMapping("/statusCode5xx")
    public ResponseEntity<String> returnStatusCode5XX() {
        return new ResponseEntity<String>(" error ", HttpStatus.SERVICE_UNAVAILABLE);
    }

    @RequestMapping("/timeout")
    public String returnTimeout() {
        try {
            Thread.sleep(9*1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return " ok ";
    }

Reproduced in: https: //www.jianshu.com/p/b40f0b13bce4

Guess you like

Origin blog.csdn.net/weixin_33738555/article/details/91054862