Search code examples
springspring-bootexceptionjersey-clientspring-resttemplate

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


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();
}

Solution

  • 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();