How to Spring Retryable with http status code

Viewed 775

I'm currently using spring 2 with restTemplate and I would like to have a retry sending a post request only with a certain status codes or any of code 500.

How do I do this?

Here is my code

@Retryable(value = RestClientException.class, exclude = {UnknownHostException.class},
           backoff=@Backoff(delayExpression = 10000,
                            multiplierExpression = 2,
                            maxDelayExpression = 50000))
    public HttpStatus postRequest(final File file) throws RestClientException {
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", new FileSystemResource(file));

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.set("Token", mytoken);

        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
        ResponseEntity<String> response = restTemplate.exchange(URI, HttpMethod.POST,requestEntity, String.class);

        return response.getStatusCode();
    }
0 Answers
Related