RestTemplate Status Code Handling

I was writing a  Spring Boot microservice that called an external service. If the service returned a `500`, the payload of the response had an error code that we wanted to send back to the service that called us. I was using RestTemplate so I thought I could just use:

if (responseEntity.getStatusCode().is2xxSuccessful()){
// happy path
}
if (responseEntity.getStatusCode().is5xxServerError()){
// do the error thing
}

But it’s not that easy. It seems that if the RestTemplate gets anything but a 2xx response, Spring’s default error handler will throw an exception. You won’t even get a chance to do anything with your ResponseEntitybecause the exception happens before that. I even found a recent bug report on the matter. Anyway, I had a chicken and an egg problem – I couldn’t parse the exception out because I didn’t have an entity for it and I didn’t have an entity because the response wasn’t a success. Of course, I may have to accept more status codes than just a 200 or a 500, so I wanted it to be configurable. This old blog entry led me in the right direction of making my own ErrorHandler. What I ended up doing was making an ErrorHandler where I injected the statuses I wanted to make.

I made a simple (and contrived) example on github. It’s actually very simple when you think about it.

My properties file looked like this:

good-status=OK,NOT_ACCEPTABLE

 

I used the words instead of the error codes. You could obviously change it.

The meat is in the MyResponseErrorHandler class. I injected the property is and then simply tested to see if the error existed:

@Component
public class MyResponseErrorHandler implements ResponseErrorHandler {
    private static final Logger log = LoggerFactory.getLogger(MyResponseErrorHandler.class);
 
    private List acceptableStatus;
 
    public  MyResponseErrorHandler(@Value("${good-status}") String goodStatus) {
 
        acceptableStatus = Arrays.stream(goodStatus.split(","))
                .map(HttpStatus::valueOf)
                .collect(Collectors.toList()) ;
 
 
    }
 
    @Override
    public void handleError(ClientHttpResponse response) throws IOException {
        log.error("Response error: {} {}", response.getStatusCode(), response.getStatusText());
    }
 
    @Override
    public boolean hasError(ClientHttpResponse response) throws IOException {
        return !acceptableStatus.contains(response.getStatusCode());
 
    }
}

And then I just inject the error handler in my client class and use it to construct the RestTemplate

 @Autowired
    public MyConsumerService(RestTemplateBuilder restTemplateBuilder,
                             MyResponseErrorHandler myResponseErrorHandler
                             ) {
 
        this.restTemplate = restTemplateBuilder
                   .errorHandler(myResponseErrorHandler)
                   .build();
    }

And that’s it – I didn’t do anything else.

The bug I referenced above said that this will be easier to do with WebClient in Spring 5. Of course, that means Spring Boot 2 (which, as I write this, is finally on RC1). So I hope that this will help others that are working in Spring Boot 1.5 for a while.

About the Author

Mike Hostetler profile.

Mike Hostetler

Principal Technologist

Mike has almost 20 years of experience in technology. He started in networking and Unix administration, and grew into technical support and QA testing. But he has always done some development on the side and decided a few years ago to pursue it full-time. His history of working with users gives Mike a unique perspective on writing software.

One thought on “RestTemplate Status Code Handling

  1. Red says:

    Thank you for sharing this good idea

  2. Jason says:

    Thank you for sharing this as well. Was encountering the same issue you were. Like you said, now that you showed the solution it actually is pretty simple but couldn’t figure it out for the life of me.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
An Exploration in Rust: Musings From a Java/C++ Developer
Why Rust? It’s fast (runtime performance) It’s small (binary size) It’s safe (no memory leaks) It’s modern (build system, language features, etc) When Is It Worth It? Embedded systems (where it is implied that interpreted […]
Getting Started with CSS Container Queries
For as long as I’ve been working full-time on the front-end, I’ve heard about the promise of container queries and their potential to solve the majority of our responsive web design needs. And, for as […]
Simple improvements to making decisions in teams
Software development teams need to make a lot of decisions. Functional requirements, non-functional requirements, user experience, API contracts, tech stack, architecture, database schemas, cloud providers, deployment strategy, test strategy, security, and the list goes on. […]
JavaScript Bundle Optimization – Polyfills
If you are lucky enough to only support a small subset of browsers (for example, you are targeting a controlled set of users), feel free to move along. However, if your website is open to […]