1

I am trying to consume api with two different responses. when providing proper input and headers, it gives one json object. That is working fine with code given below.

but when one of the inputs or headers are missing then api is returning other json which states error details while hitting through Postman but same is not getting achieved by client code. It throws exception but api should return json with error details.

first I tried with postForobject(), then changed to exchange() assuming postForObject returning object and api is not getting expected object format. so tried with String.clas instead of particular class

what can be done to get two different json object by hitting same url?

when success:

{
    "contacts": [
        {
            "input": "98########",
            "status": "valid"
        }
    ]

}

when input or header missing:

"errors": [
        {
            "code": 1###,
            "title": "Access denied",
            "details": "invalid deatils"
        }
    ]

below is my code:

 public static void main(String[] args) throws Exception {

        RestTemplate restTemplate = new RestTemplate();
        String check_Contact_Async = "https://port/contacts/";

        sslByPass();
        //headers = setHeaders();
        contactsDTO = getInput(); 

        final HttpEntity<ContactsDTO> entity = new HttpEntity<ContactsDTO>(contactsDTO, headers);

        try {
            //WsResponse res = restTemplate.postForObject(check_Contact_Async, entity, WsResponse.class);

            ResponseEntity<String> responseEntity = restTemplate.exchange(
                    check_Contact_Async, HttpMethod.POST, entity,
                       String.class);

            System.out.println(responseEntity);
        } catch (Exception exception) {

            System.out.println(exception);
        } 
    }



Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:667)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:620)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:498)
    at com.websystique.springmvc.apitest.Example.main(Example.java:120)

any clue will be helpful

Thanks

Sonal
  • 262
  • 5
  • 22
  • 1
    Possible duplicate of [How to use RestTemplate with multiple response types?](https://stackoverflow.com/questions/35797762/how-to-use-resttemplate-with-multiple-response-types) – jonrsharpe Jun 08 '18 at 06:39
  • @jonrsharpe already checked that link and then changed it to responseEntity with string class. but did not help. – Sonal Jun 08 '18 at 06:49
  • The `RestTemplate.exchange` method throws an RestClientException on non-successful status code response. See https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html#exchange-org.springframework.http.RequestEntity-java.lang.Class- – MvdD Jun 09 '18 at 17:17

0 Answers0