gpt4 book ai didi

json - @XmlJavaTypeAdapter 不适用于 Jackson JSON

转载 作者:行者123 更新时间:2023-12-02 21:07:43 27 4
gpt4 key购买 nike

我正在使用 Spring REST 框架和 Jackson JSON

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="customObjectMapper" />
<property name="supportedMediaTypes" value="application/json" />
</bean>
<bean id="customObjectMapper"class="com.rackspace.payment.webapp.utils.JaxbJacksonObjectMapper" />

public class JaxbJacksonObjectMapper extends ObjectMapper {
@SuppressWarnings("deprecation")
public JaxbJacksonObjectMapper() {
final AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();

this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
super.getDeserializationConfig().withAnnotationIntrospector(introspector);

this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRITE_NULL_PROPERTIES, false);
this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
super.getSerializationConfig().withAnnotationIntrospector(introspector);
}
}


//JXAB element
@XmlAttribute(name = "createDate")
@XmlJavaTypeAdapter(CustomDateAdapter.class)
protected Date createDate;

我面临的问题是,当显示 JSON 输出时,“createDate”元素显示为字符串,不会调用 xml 适配器类,而它在 XML 输出中工作正常。

我错过了什么?如何以 JSON 格式调用 XmlJavaTypeAdapter。

最佳答案

下面的代码对我有用。

    ObjectMapper objectMapper = new CustomObjectMapper(ENTITY_WITH_ID_DESERIALIZER);

AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
AnnotationIntrospector secondary = new JaxbAnnotationIntrospector();
AnnotationIntrospector pair = new AnnotationIntrospector.Pair(primary, secondary);

objectMapper.setAnnotationIntrospector(pair);

objectMapper.configure(DeserializationConfig.Feature.USE_ANNOTATIONS, true);

关于json - @XmlJavaTypeAdapter 不适用于 Jackson JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12946547/

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