9

Facing issues while parsing a file and converting into POJO. Below exception I am getting.

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.util.LinkedHashMap: no String-argument constructor/factory method to deserialize from String value ('{\"hosturl_path\":\"/images\"}')

Sample json file:

{"test": [{
     "a112a": "testhost", 
     "a112b": "{\"hosturl_path\":\"/images\"}"
}]}

POJO -

import java.io.Serializable;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, getterVisibility = JsonAutoDetect.Visibility.ANY,
        setterVisibility = JsonAutoDetect.Visibility.ANY)
public class TestPojo implements Serializable
{
    private static final long serialVersionUID = 638312139361412L;

    @JsonProperty("a112a")
    private String host;

    @JsonProperty("a112b")
    private Map<String,String> parameterMap;

    public TestPojo()
    {
    }

    public TestPojo(String host, Map<String, String> parameterMap)
    {
        this.host = host;
        this.parameterMap = parameterMap;
    }

    public String getHost()
    {
        return host;
    }

    public void setHost(String host)
    {
        this.host = host;
    }

    public Map<String, String> getParameterMap()
    {
        return parameterMap;
    }

    public void setParameterMap(Map<String, String> parameterMap)
    {
        this.parameterMap = parameterMap;
    }
}

Main class

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;


public class TestApp
{
    public static void main(String[] args) throws IOException
    {
        ObjectMapper mapper = new ObjectMapper();

        String fileName = "sampleFile.json";

        URL resource = TestApp.class.getResource(fileName);

        Map<String,List<TestPojo>> map = mapper.readValue(resource, new
                TypeReference<Map<String, List<TestPojo>>>()
                {
                });

        System.out.println(map);
    }
}
3
  • 1
    What's your question? Do you understand why you're getting the exception? Commented Aug 15, 2018 at 3:54
  • Have made changes in json file. Please check now. Commented Aug 15, 2018 at 4:37
  • Ok... you still have a string value instead of an object. Commented Aug 15, 2018 at 4:38

1 Answer 1

7

Your exception is telling you exactly what is going on here.

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.util.LinkedHashMap: no String-argument constructor/factory method to deserialize from String value ('{\"hosturl_path\":\"/images\"}')

In your TestPojo.class, you are attempting to deserialize a String into a Map.

@JsonProperty("a112b")
private Map<String,String> parameterMap;

If you were to adjust this to your TestPojo.class

@JsonProperty("a112b")
private String parameterMap;

You will get a successful run. However, I would imagine that this isn't the desired behavior. If you have the ability to change your sampleFile Jackson should be able to deserialize that correctly.

{"test": [{
 "a112a": "testhost", 
 "a112b": {"hosturl_path":"images"}
}]}

Otherwise. You will need to specify a custom deserializer.

@JsonDeserialize(CustomParameterDeserializer.class)
@JsonProperty("a112b")
private Map<String,String> parameterMap;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.