Home » Java » http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_OBJECT token

http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_OBJECT token

Posted by: admin July 12, 2018 Leave a comment

Questions:

I am trying to read json request and then creating response. Request belongs to “Request.java” and Response belongs to “WsResponse.java”. WsResponse.java is linked to contacts.java. When using postman appropriate response is achieved. but when trying to call method through client call, it is giving exception

"Exception in thread "main" org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: java.io.PushbackInputStream@44c8afef; line: 1, column: 14] (through reference chain: com.websystique.springmvc.model.Request["contacts"]->java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: java.io.PushbackInputStream@44c8afef; line: 1, column: 14] (through reference chain: com.websystique.springmvc.model.Request["contacts"]->java.util.ArrayList[0])
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:227)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:212)
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:95)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:622)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580)
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:380)
    at com.websystique.springmvc.Test.main(Test.java:49)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: java.io.PushbackInputStream@44c8afef; line: 1, column: 14] (through reference chain: com.websystique.springmvc.model.Request["contacts"]->java.util.ArrayList[0])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:835)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer._parseString(StdDeserializer.java:810)
    at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:190)
    at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:161)
    at com.fasterxml.jackson.databind.deser.std.StringCollectionDeserializer.deserialize(StringCollectionDeserializer.java:19)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:523)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:95)
    at com.fasterxml.jackson.databind.deser.impl.BeanPropertyMap.findDeserializeAndSet(BeanPropertyMap.java:285)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:335)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:142)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3562)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2662)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:224)
    ... 6 more"

Below classes are given:

public class Request {
        @JsonProperty
        private String blocking;
        @JsonProperty
        private String module;
        @JsonProperty("contacts")
        private List<String> contacts;

        public Request(){

        }

        public String getBlocking() {
            return blocking;
        }

        public void setBlocking(String blocking) {
            this.blocking = blocking;
        }

        public String getModule() {
            return module;
        }

        public void setModule(String module) {
            this.module = module;
        }

        public List<String> getContacts() {
            return contacts;
        }

        public void setContacts(List<String> contacts) {
            this.contacts = contacts;
        }

        @Override
        public String toString() {
            return "Request [blocking=" + blocking + ", module=" + module + ", contacts=" + contacts + "]";
        }
    } 



public class WsResponse {

    @JsonProperty("contacts")
    private List<contacts> contacts;

    public WsResponse() {
    }

    public List<contacts> getContacts() {
        return contacts;
    }

    public void setContacts(List<contacts> contacts) {
        this.contacts = contacts;
    }

    @Override
    public String toString() {
        return "[contacts:" + contacts + "]";
    }


public class contacts {
        private String input;
        private String status;
        private String wa_id;

        public contacts() {
        }

        public String getInput() {
            return input;
        }

        public void setInput(String input) {
            this.input = input;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getWa_id() {
            return wa_id;
        }

        public void setWa_id(String wa_id) {
            this.wa_id = wa_id;
        }

        @Override
        public String toString() {
            return "{input=" + input + ", status=" + status + ", wa_id=" + wa_id + "}";
        }
    }

public class Test {

    public static void main(String[] args) throws JSONException {
        final String uriForPost = "http://localhost:8080/Spring4MVCCRUDRestService/getContactDetails/";

        ArrayList<String> contactList = new ArrayList<String>();
        contactList.add("9315551000");
        contactList.add("9315551002");
        contactList.add("931-555-1005");
        contactList.add("031-555-1005");

        Request req = new Request();
        req.setBlocking("wait");
        req.setModule("Model_A");
        req.setContacts(contactList);

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.postForObject(uriForPost, req, Request.class);
    }
}

Here my request JSON:

  {
        "blocking": "wait",
        "module":"Model_A",
        "contacts": [    
            "7855222121",
            "988542d1222",
            "9029674116",
            "5252525252",
            "213123123123"
        ]
    }

Response (after calling it from Postman):

{
    "contacts": [
        {
            "input": "7855222121",
            "status": "valid",
            "wa_id": "7855222121"
        },
        {
            "input": "988542d1222",
            "status": "valid",
            "wa_id": "988542d1222"
        },
        {
            "input": "9029674116",
            "status": "valid",
            "wa_id": "9029674116"
        },
        {
            "input": "5252525252",
            "status": "valid",
            "wa_id": "5252525252"
        },
        {
            "input": "213123123123",
            "status": "valid",
            "wa_id": "213123123123"
        }
    ]
}

Tried below solutions which got on Stackoverflow and other sites available:

1. using @JsonProperty on fields
2. Passing JSON object instead of Request class object


JSONObject json = new JSONObject();
    json.put("blocking", "wait");
    json.put("module", "Model_A");

    ArrayList<String> contactList = new ArrayList<String>();
    contactList.add("6315551000");
    contactList.add("6315551002");
    contactList.add("631-555-1005");
    json.put("contacts", contactList);

3.Adding below code for convertor

    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
            messageConverters.add(new FormHttpMessageConverter());
            messageConverters.add(new StringHttpMessageConverter());
            //messageConverters.add(new MappingJacksonHttpMessageConverter());
            restTemplate.setMessageConverters(messageConverters);

4.

    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
            mappingJackson2HttpMessageConverter.setSupportedMediaTypes(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.APPLICATION_OCTET_STREAM));
            restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);

Any hint or clue will be helpful.

Thanks

Answers: