no String-argument constructor/factory method to deserialize from String value - Exception while deserializing json object from restTemplate

33,767

The code is correct but there's a problem with the JSON. The address is a string and not a JSON object. For it to work, it would need to be something like:

"address": {"state": "LA", "country": "US"}

Without the outer quotes and the escape characters.

Share:
33,767
Sriram G
Author by

Sriram G

Working as Lead Consultant in Technology, Full Stack Java and DevOps expert.

Updated on July 09, 2022

Comments

  • Sriram G
    Sriram G almost 2 years

    Facing issue while making call to retrieve a json response and parse it.

    [
        {
            "name": "john doe",
            "age": "24",
            "address": "{\"state\":\"LA\",\"country\":\"US\"}"
        }
    ]
    

    Models:

    Person.java

    @Data
    @NoArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true) 
    public class Person {
        private String name;
        private String age;
        private Address address;
    }
    

    Address .java

    @Data
    @NoArgsConstructor
    @JsonIgnoreProperties(ignoreUnknown = true) 
    public class Address {
        private String state;
        private String country;
    }
    

    Code to read this data,

    ResponseEntity<List<Person>> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET,requestEntity,new ParameterizedTypeReference<List<Person>>() {});
    

    However i get below exception,

    RestClientException while invoking ABS ServiceError while extracting response for type [java.util.List<com.bp.model.Person>] and content type [application/json;charset=UTF-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of com.bp.model.Address (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"state":"LA","country":"US"}'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.bp.model.Address (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"state":"IN","brand":"anthem"}') at [Source: (PushbackInputStream); line: 1, column: 325] (through reference chain: java.util.ArrayList[0]->com.bp.model.Person["address"])