Priyantha
Priyantha

Reputation: 5083

How do I read HTTP status code from RestTemplate?

I call Rest API using RestTemplate. Response don't has any issues.I need to get HTTP status code Like this.

Request is ok- 200

Unauthorized - 400

According to many post I met bellow answer.

RestTemplate template = new RestTemplate();
HttpEntity<String> response = template.exchange(url, HttpMethod.POST, request, String.class);

String resultString = response.getBody();
HttpHeaders headers = response.getHeaders();

In here used exchange method to get response.(template.exchange....) But I used

postForObject method

This is my code

public BalanceCheckResponse accountTotal(String counterAlias, String counterAuth, String subscriberType,
        String subscriberValue, String accessMode, String token) {
    
    BalanceCheckResponse objResponse = new BalanceCheckResponse();

    try {
        
        BalanceCheckRequest objRequest = new BalanceCheckRequest();
        objRequest.setCounterAlias(counterAlias);
        objRequest.setCounterAuth(counterAuth);
        objRequest.setSubscriberType(subscriberType);
        objRequest.setSubscriberValue(subscriberValue);
        objRequest.setAccessMode(accessMode);

        RestTemplate rstTemp = new RestTemplate();
        final String url = "https://ideabiz.lk/apicall/startpoint/v1/balanceCheck";
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        MappingJackson2HttpMessageConverter map =new MappingJackson2HttpMessageConverter();
        messageConverters.add(map);
        //messageConverters.add(new StringHttpMessageConverter());
        rstTemp.setMessageConverters(messageConverters);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Authorization", "Bearer "+token);

        HttpEntity<BalanceCheckRequest> entity = new HttpEntity<BalanceCheckRequest>(objRequest,headers);
        objResponse = rstTemp.postForObject(url,entity, BalanceCheckResponse.class  );
        
        
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(objResponse.getCurrentBalance());
    return objResponse;

}

In my code

objResponse.getHeaders();

show error. What can I do in here. Do you have idea?

Upvotes: 1

Views: 13316

Answers (2)

Mike Nakis
Mike Nakis

Reputation: 61993

By default, RestTemplate uses a DefaultResponseErrorHandler, which throws an exception if you do not receive back one of the responses that are considered as successful. This means that in the event of an error, your e.printStackTrace(); statement will print a stack trace. (Which will most probably go unnoticed.)

If that's not good enough for you, then there is a number of ways to circumvent that behavior, one of them is by supplying your own error handler using setErrorHandler( ResponseErrorHandler errorHandler ).

Upvotes: 5

Bewusstsein
Bewusstsein

Reputation: 1621

The return value of the postForObject method is the data from the received response that is deserialized to the given class, in your case BalanceCheckResponse.

If you need access to both returned data and status, use postForEntity like this:

ResponseEntity<BalanceCheckResponse> responseEntity = rstTemp.postForEntity(url, entity, BalanceCheckResponse.class);
objResponse = responseEntity.getBody();
HttpStatus status = responseEntity.getStatusCode();

Upvotes: 3

Related Questions