In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to deal with RestTemplate custom request failure exceptions. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
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.
@ Testpublic 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 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 public void handleError (ClientHttpResponse response) throws IOException {/ / you can implement your own encounter with Error for reasonable processing / / TODO to persist the exception information requested by the API}}
Register MyRestErrorHandler when RestTemplate is instantiated.
Reference:
RestTemplate uses intensive speaking in Spring or non-Spring environments
RestTemplate realizes the switching usage of multiple underlying HTTP client class libraries
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.
These are all the contents of the article "how to deal with RestTemplate Custom request failure exceptions". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.