Make is easier to use TestRestTemplate with a custom ResponseErrorHandler
See original GitHub issueCustom error handler set using RestTemplateBuilder
bean or using the configuration TestRestTemplateConfiguration
from Spring Boot Reference Guide is overriden by TestRestTemplate.NoOpResponseErrorHandler
.
Specifically, when using the following class:
class CustomResponseErrorHandler extends DefaultResponseErrorHandler {
// implementation
}
in the configuration of the builder:
ResponseErrorHandler customErrorHandler = new CustomResponseErrorHandler();
RestTemplateBuilder builder = builderProvider.getIfAvailable()
.errorHandler(customErrorHandler);
The following line is false:
new TestRestTemplate(builder.build()).getRestTemplate().getErrorHandler().equals(customErrorHandler)
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Spring Resttemplate exception handling - Stack Overflow
You want to create a class that implements ResponseErrorHandler and then use an instance of it to set the error handling of your...
Read more >RestTemplate Error Handling Example - Apps Developer Blog
Create a custom exception that we want to throw,; To enable RestTemplate to handle errors, we will implement ResponseErrorHandler interface, ...
Read more >Spring RestTemplate Error Handling - Baeldung
In this short tutorial, we'll discuss how to implement and inject the ResponseErrorHandler interface in a RestTemplate instance to ...
Read more >Spring Boot RestTemplate Error Handling
The simplest way to add a custom error handler is to use a try-catch block to catch the HttpStatusCodeException exception.
Read more >Complete Guide to Spring RestTemplate - Reflectoring
In this tutorial, we will understand how to use RestTemplate for ... each of the HTTP methods which make it easy to invoke...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
TestRestTemplate doesn’t actually offer what @bclozel describes. I am writing my unit tests, using TestRestTemplate with a ResponseType of “UUID”. When I test BAD_REQUEST errors from my service, instead of my being able to inspect the ResponseEntity and check for status “401”, TestRestTemplate throws an exception saying it can’t decode the error message as a UUID. I don’t understand why TestRestTemplate is even attempting to decode the ResponseType when a 4xx error is returned.
@cs94njw I don’t understand how this is related. This looks like a different problem? Could you report that separately with a sample project (something we can git clone or download) we can take a look at?