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);
}
}
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With