gpt4 book ai didi

java - @XmlElementWrapper 和@XmlElement 无法使用 Jackson 进行反序列化

转载 作者:搜寻专家 更新时间:2023-10-31 20:25:28 27 4
gpt4 key购买 nike

我正在使用 jackson-databind:2.9.3 和 jackson-module-jaxb-annotations:2.9.3

我有以下格式的 JSON。

{
"response": {
"data": {
"entry": {
"name": "xxxxxxxxxx"
}
}
}
}

此 JSON 响应需要反序列化为具有以下结构的类。

@XmlRootElement(name = "response")
public class Response {

private List<Entry> entry;

public Response() {
}

public Response(List<Entry> entry) {
this.entry = entry;
}

@XmlElementWrapper(name = "data")
@XmlElement(name = "entry")
public List<Entry> getData() {
return this.entry;
}

public void setData(List<Entry> entry) {
this.entry = entry;
}
}

public class Entry {

private String name;

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}
}

为了反序列化,我尝试了以下代码。但它不起作用。

    import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector

ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);


mapper.configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true);
mapper.configure(MapperFeature.USE_ANNOTATIONS, true);

AnnotationIntrospector aiJaxb = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
AnnotationIntrospector aiJackson = new JacksonAnnotationIntrospector();
mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(aiJaxb, aiJackson));

System.out.println(mapper.readValue(s, Response.class));

但是得到以下异常

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "entry" (class Entry), not marked as ignorable

at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:60)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownProperty(DeserializationContext.java:822)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:1152)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1567)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownVanilla(BeanDeserializerBase.java:1545)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:293)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:352)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:264)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:244)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:127)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:287)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
at com.fasterxml.jackson.databind.ObjectMapper._unwrapAndDeserialize(ObjectMapper.java:4081)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3999)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)

......

不确定我错过了什么。任何帮助,将不胜感激。提前致谢。

最佳答案

尝试添加这个:

mapper.registerModule(new JaxbAnnotationModule());

关于java - @XmlElementWrapper 和@XmlElement 无法使用 Jackson 进行反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48022952/

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