我在stackoverflow上已经阅读了很多关于这个问题但是任何解决方案都适用于我。任何人都可以告诉我为什么它没有工作,因为我已经描述了所有孩子正确对象 - 或者可能没有?
这是我对杰克逊的依赖
<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>
使用我的抽象对象作为属性的主类(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;
}
}
抽象类实现
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();
}
}
子类实现
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;
}
}
和json文本我要阅读 - 这只是对真实信息的测试。
String msg = "{\"errorCode\": 0,\"errorMessage\":\"\",\"object\":{\"type\":\"person\",\"name\":\"Jan\",\"secondName\":\"Jan\"}}";
我尝试使用以下代码阅读
ObjectMapper objectMapper = new ObjectMapper();
MLMResponse emp = objectMapper.readValue(msg, MLMResponse.class);
但是当我运行它时,我会得到以下错误。
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)
有人可以帮帮我吗?
此致 扬
答案 0 :(得分:8)
同步你的杰克逊版本。杰克逊1寻找杰克逊1注释,杰克逊2寻找杰克逊2注释。你正试图让杰克逊1寻找杰克逊2注释。这根本不会起作用。