1

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?

Community
  • 1
  • 1
Priyantha
  • 4,839
  • 6
  • 26
  • 46
  • Hi, where do you call `objResponse.getHeaders();`? It looks like `objResponse`is an `BalanceCheckRequest` object? – kkflf Mar 28 '17 at 11:12
  • Its not. objResponseis is an BalanceCheckResponse object.I called it under objResponse = rstTemp.postForObject(url,entity, BalanceCheckResponse.class ); line.Thank you for your comment kkflf – Priyantha Mar 28 '17 at 11:14

2 Answers2

5

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 ).

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • Thank you Mike Nakis.I already used e.printStackTrace();. But I needed to get that error with efferent way. – Priyantha Mar 28 '17 at 11:32
3

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();
Bewusstsein
  • 1,591
  • 13
  • 19