gpt4 book ai didi

java - Json 和抽象类 'Can not construct instance'

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:20:28 24 4
gpt4 key购买 nike

我在 stackoverflow 上阅读了很多有关此问题的信息,但任何解决方案都适合我。任何人都可以告诉我为什么它不起作用,因为我已经正确地描述了所有 child 对象 - 或者可能不正确?

这是我对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>

以我的抽象对象作为属性的主类 (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)

有人可以帮我吗?

问候简

最佳答案

同步您的 Jackson 版本。 Jackson 1 查找 Jackson 1 注释,Jackson 2 查找 Jackson 2 注释。您正在尝试让 Jackson 1 查找 Jackson 2 注释。这根本行不通。

关于java - Json 和抽象类 'Can not construct instance',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22989887/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com