Erdogan
Erdogan

Reputation: 65

Response Entity is null if the HTTPStatus Code is not equal to 200

I just started to use the Spring Framework and making some tests. I'm trying to reach ResponseEntity. But if the HTTP status code is not equal to 200, the ResponseEntity object is always null. Therefore I cannot reach the status code or headers.

Is there any way to reach ResponseEntity if the HTTP status code is not equal to 200?

public static String checkPatientExistence(String pNo, String eNo) throws RestClientException, NullPointerException {
    String result = null;
    RestTemplate restTemplate = createRestTemplate();
    final String uri = apiURL + "GetInstance";

    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(uri)
            .queryParam("pNo", pNo)
            .queryParam("eNo", eNo);

    HttpHeaders headers = new HttpHeaders();
    headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);

    HttpEntity<String> requestEntity = new HttpEntity<>(headers);

    ResponseEntity<String> responseEntity = null;
    try {
        responseEntity = restTemplate.exchange(
                builder.toUriString(),
                HttpMethod.GET,
                requestEntity,
                String.class
        );
    } catch (RestClientException restClientException) {
        System.out.println(restClientException.getMessage());
        System.out.println(Arrays.toString(restClientException.getStackTrace()));
    }

    if (responseEntity != null) {
        if (responseEntity.getStatusCode() == HttpStatus.OK) {
            result = "true";
        } else if (responseEntity.getStatusCode() == HttpStatus.BAD_REQUEST) {
            System.out.println(responseEntity.getStatusCode().toString());
            System.out.println(responseEntity.getBody());
            result = "false";
        } else if (responseEntity.getStatusCode() == HttpStatus.NO_CONTENT) {
            System.out.println(responseEntity.getStatusCode().toString());
            System.out.println(responseEntity.getBody());
            result = "noContent";
        }
    } else if (responseEntity == null) {
        System.out.println("responseEntity is null");
        result = "false";
    }
    return result;
}

Here is the output of the method:

400 Bad Request: [{"Message":"error message"}]
[org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:101),
org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:170),
org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:112),
org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63),
org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:782),
org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:740),
org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674),
org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:583),
com.xxx.xxx.App.checkPatientExistence(App.java:269), com.xxx.xxx.App.main(App.java:43)]
responseEntity is null
false

Upvotes: 1

Views: 3956

Answers (2)

Erdogan
Erdogan

Reputation: 65

RestClientException doesn't have methods like getStatusCode() or getResponseBodyAsString(). Because of that I couldn't reach them in the catch block.

Therefore, I added another catch block for HttpsStatusCodeException and reversed the order of catch blocks.

The HttpStatusCodeException

extends RestClientResponseException

and the RestClientResponseException

extends RestClientException

Here is the sample methods we can use.

} catch (HttpStatusCodeException httpStatusCodeException) {
    System.out.println(httpStatusCodeException.getStatusCode());
    System.out.println(httpStatusCodeException.getResponseBodyAsString());
    System.out.println(httpStatusCodeException.getStatusText());
    System.out.println(httpStatusCodeException.getRawStatusCode());
    System.out.println(httpStatusCodeException.getMessage());
} catch (RestClientException restClientException) {
    // catch
}

Upvotes: 1

Dinesh Narayan
Dinesh Narayan

Reputation: 105

You already have your response for status codes other than 200 in catch block. Define your ResponseEntity object inside catch block.

    catch (HttpStatusCodeException exception) {
              //Add data to responseEntity
        }

Upvotes: 0

Related Questions