I have read a lot about this problem on stackoverflow but any solution works for me. Could anybody please tell me why it's not working since I have described all children object properly - or maybe not ?
This are my dependency for jackson
<context:annotation-config/>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.10</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.3.2</version>
</dependency>
Main class with my abstract object as property (MLMObject)
public class MLMResponse {
private int errorCode;
private String errorMessage;
private MLMObject object;
public MLMResponse() {
}
public int getErrorCode() {
return errorCode;
}
public void setErrorCode(int errorCode) {
this.errorCode = errorCode;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public MLMObject getObject() {
return object;
}
public void setObject(MLMObject object) {
this.object = object;
}
}
abstract class implementation
import com.fasterxml.jackson.annotation.*;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value=Person.class, name="person"),
})
public abstract class MLMObject {
public MLMObject() {
super();
}
}
Child class implementation
import com.fasterxml.jackson.annotation.JsonTypeName;
@JsonTypeName("person")
public class Person extends MLMObject{
private String name;
private String secondName;
public Person() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
}
and json text I'm going to read - this is only test for real message.
String msg = "{\"errorCode\": 0,\"errorMessage\":\"\",\"object\":{\"type\":\"person\",\"name\":\"Jan\",\"secondName\":\"Jan\"}}";
I'm trying to read it with following code
ObjectMapper objectMapper = new ObjectMapper();
MLMResponse emp = objectMapper.readValue(msg, MLMResponse.class);
But when I run it I'm getting following error.
org.codehaus.jackson.map.JsonMappingException: Can not construct instance of com.pl.model.MLMObject, problem: abstract types can only be instantiated with additional type information
at [Source: java.io.StringReader@a06816; line: 1, column: 34] (through reference chain: com.pl.model.MLMResponse["object"])
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.instantiationException(StdDeserializationContext.java:233)
at org.codehaus.jackson.map.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:60)
at org.codehaus.jackson.map.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:299)
at org.codehaus.jackson.map.deser.SettableBeanProperty$MethodProperty.deserializeAndSet(SettableBeanProperty.java:414)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:697)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863)
at com.pl.service.JacksonMappingService.mapLoginMessage(JacksonMappingService.java:22)
at com.pl.controller.HelloWorldController.hello(HelloWorldController.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
Could anybody please help me with that ?
Regards Jan
Synchronize your Jackson versions. Jackson 1 looks for Jackson 1 annotations and Jackson 2 looks for Jackson 2 annotations. You are trying to have Jackson 1 look for Jackson 2 annotations. This simply won't work.
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