In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "RestTemplate custom request failure exception handling", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "RestTemplate custom request failure exception handling" it!
I. abnormal phenomena
When using RestTemplate for remote interface service invocation, the following exception is thrown when the requested service times out, the service does not exist, etc. (the response status is not 200,400, but 500HTTP status code):
I simulated the exception and changed the correct request service address from "/ posts/1" to "/ postss/1". The service does not exist, so a 404 exception is thrown.
[@ Test] (https://my.oschina.net/azibug)public void testEntity () {String url = "http://jsonplaceholder.typicode.com/postss/1"; ResponseEntity responseEntity = restTemplate.getForEntity (url, String.class); / / this line throws an exception / / the following two lines of code cannot execute HttpStatus statusCode = responseEntity.getStatusCode (); / / get the response code System.out.println (" HTTP response status: "+ statusCode);}
After the exception is thrown, the code behind the program cannot be executed, and the later code cannot be executed. In actual business development, sometimes we prefer the result to be: whether your server times out or the service does not exist, we should get the final request result (HTTP request result status 400,500) rather than a thrown exception.
2. Source code parsing-default implementation
First of all, I would like to make a conclusion: RestTemplate request result exceptions can be customized. Before we start with custom exception handling logic, it's important to take a look at the default implementation of exception handling. That is: why does it have the phenomenon mentioned in the above section?
ResponseErrorHandler is the exception handler interface for RestTemplate request results.
The first method of the interface, hasError, is used to determine whether the HttpResponse is an abnormal response (through the status code)
The second method of the interface, handleError, is used to handle the exception response result (non-200status code segment)
DefaultResponseErrorHandler is the default implementation of ResponseErrorHandler
So let's take a look at how DefaultResponseErrorHandler handles abnormal responses. HttpStatusCode is parsed from HttpResponse, and if the status code StatusCode is null, a UnknownHttpStatusCodeException exception is thrown.
If StatusCode exists, the series of the StatusCode is parsed, that is, the status code segment (except for 200 segments, all are exception status codes), and the parsing rule is StatusCode/100 rounding.
Public enum Series {INFORMATIONAL (1), / / 1xx/100 SUCCESSFUL (2), / / 2xx/100 REDIRECTION (3), / / 3xx/100 CLIENT_ERROR (4), / / 4xx/100, client exception SERVER_ERROR (5); / / 5xx/100, server exception}
Further handling of client-side and server-side exceptions is done by throwing HttpClientErrorException. That is the reason for the exception in the first section.
3. RestTemplate custom exception handling
So if we want to implement a custom exception, we can implement the ResponseErrorHandler interface.
Public class MyRestErrorHandler implements ResponseErrorHandler {/ * determine whether the returned result response is an abnormal result * mainly to check the HTTP Status of response * to copy the DefaultResponseErrorHandler implementation * / [@ Override] (https://my.oschina.net/u/1162528) public boolean hasError (ClientHttpResponse response) throws IOException {int rawStatusCode = response.getRawStatusCode (); HttpStatus statusCode = HttpStatus.resolve (rawStatusCode); return (statusCode! = null? StatusCode.isError (): hasError (rawStatusCode));} protected boolean hasError (int unknownStatusCode) {HttpStatus.Series series = HttpStatus.Series.resolve (unknownStatusCode); return (series = = HttpStatus.Series.CLIENT_ERROR | | series = = HttpStatus.Series.SERVER_ERROR) } [@ Override] (https://my.oschina.net/u/1162528) public void handleError (ClientHttpResponse response) throws IOException {/ / you can implement your own encounter with Error for reasonable handling / / TODO to persist the exception information requested by the API}}
Register MyRestErrorHandler when RestTemplate is instantiated.
If you execute the sample code in the first section at this point, an exception will not be thrown. Instead, we get a result of HTTP Status 404. Based on this result, we can continue to execute the code down in the program.
At this point, I believe you have a deeper understanding of "RestTemplate custom request failure exception handling". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.