0

I have the following code to get response as String and Object

try (CloseableHttpClient httpClient = getHttpClient();
   CloseableHttpResponse response = httpClient.execute(target, post)) {
   String responseString = EntityUtils.toString(response.getEntity());
   ResponseVO responseVO = new ObjectMapper().readValue(responseString, ResponseVO.class);

It's working for specific URL, after changed the URL, I'm getting the following error (getting empty response):

com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input
 at [Source: ; line: 1, column: 0]
        at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:255)
        at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:3851)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3792)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2797)

My ResponseVO:

@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseVO {
    @JsonProperty("isBlocked")
    private boolean blocked;
    private String responseId;
    //getters and setters

Do I need to add checks on responseString? check only not empty? what about invalid response? can/should I create an empty object in case of an error?

Other answers are not relevant to my case

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • I think it is happening because the `responseString` is returning `null` so the mapping of JSON string to Java objects cannot happen. Maybe you need to add a null check. – robot_alien Dec 18 '18 at 09:37
  • @robot_alien it's empty, can I handle invalid input using ObjectMapper properties? – Ori Marko Dec 18 '18 at 09:42

0 Answers0