Migrating from Jersey client to RestTemplate, but it catch 401 error as HttpClientErrorException but Jersey client was not throwing this error why?

256 views Asked by At

In my Service there was Jersey client implementation to call a rest API now I was migrating this code to RestTemplate. In old code when there was a 401 error that comes as a response from the backend and I store the response in an object.

But when I migrated the code to RestTeplate the 401 is caught by HttpClientErrorException class so I am not able to get the response since the code flow goes to the catch block.

Jersey Client code

public Employees getEmployees1() throws MyException {
        Employee employee=new Employee(23, "Test", "Test", "[email protected]");
        
        ClientResponse response=null;
        try {
            Client client = Client.create();
            WebResource webResource = client.resource("http://localhost:8080/employees/");
            response = webResource.accept("application/json")
                    .type("application/json").header("Authorization", "invalid Data").post(ClientResponse.class, employee);

        }catch (RuntimeException e) {
            logger.error("Runtime Error Occured -{} Response - {} ",e.getMessage(),response.getStatus());
            throw new MyException("Unexpected Error Occured",e);
        }catch (Exception e) {
            logger.error("Some Error Occured -{} Response - {} ",e.getMessage(),response.getStatus());
            throw new MyException("Unexpected Error Occured",e);
        }
        return response.readEntity(Employees.class);
    }

RestTemplate Code

public Employees getEmployees() throws MyException {
    Employee employee=new Employee(23, "Test", "Test", "[email protected]");
    HttpHeaders headers=new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.add(HttpHeaders.AUTHORIZATION, "invalid Data");
    ResponseEntity<Employees> response=null;
    try {
        response = this.restTemplate.exchange("http://localhost:8080/employees/", HttpMethod.POST, new HttpEntity<Employee>(employee,headers), Employees.class);
    }catch (RuntimeException e) {
        logger.error("Runtime Error Occured -{} Response - {} ",e.getMessage(),response.getStatusCode());
        throw new MyException("Unexpected Error Occured",e);
    }catch (Exception e) {
        logger.error("Some Error Occured -{} Response - {} ",e.getMessage(),response.getStatusCode());
        throw new MyException("Unexpected Error Occured",e);
    }
    return response.getBody();
}
2

There are 2 answers

0
Nalin Kumar On BEST ANSWER

I used the default ResponseErrorHandler, by using this it will bypass all the ResponseError exception

RestTemplate rt =  restTemplateBuilder.errorHandler(new ResponseErrorHandler(){
            @Override
            public boolean hasError(ClientHttpResponse response) throws IOException {
                return false;
            }
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
            }})
        .build();
3
silh On

By default RestTemplate throws an HttpStatusCodeException (a child of it) for all 400+ status codes - see DefaultResponseErrorHandler. You can change this behavior by setting your own implementation of ResponseErrorHandler to RestTemplate using setErrorHandler or if RestTemplate is constructed using RestTemplateBuilder - using errorHandler method of the builder.