2

我有一个用例,我需要维护两组 JSON 输出,一组带有漂亮的 JSON 属性名称,另一组没有。所以我决定自定义我的 ObjectMapper 以便它忽略字段上的 @JsonProperty("pretty name") 注释并使用字段属性名称。在这种情况下想要以下 JSON 输出

{"result":{"data":[{"totalUsers":12345,"totalBooks":883}]}}

具有漂亮名称的 JSON 输出如下所示

{"result":{"data":[{"Total Users":12345,"Total Books":883}]}}

我的 ObjectMapper 配置代码看起来像

ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Include.NON_EMPTY);
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.configure(MapperFeature.USE_WRAPPER_NAME_AS_PROPERTY_NAME, true);
mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
mapper.configure(MapperFeature.USE_ANNOTATIONS, false);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.NONE)
        .setVisibility(PropertyAccessor.GETTER, Visibility.ANY);

我查看了其他一些对我不起作用的 SO 答案。我得到一个NPE。请参阅以下堆栈跟踪

java.lang.NullPointerException
at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector._renameWithWrappers(POJOPropertiesCollector.java:728)
at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.collect(POJOPropertiesCollector.java:264)
at com.fasterxml.jackson.databind.introspect.BasicClassIntrospector.collectProperties(BasicClassIntrospector.java:142)
at com.fasterxml.jackson.databind.introspect.BasicClassIntrospector.forSerialization(BasicClassIntrospector.java:68)
at com.fasterxml.jackson.databind.introspect.BasicClassIntrospector.forSerialization(BasicClassIntrospector.java:11)
at com.fasterxml.jackson.databind.SerializationConfig.introspect(SerializationConfig.java:490)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:131)
at com.fasterxml.jackson.databind.ser.SerializerFactory.createSerializer(SerializerFactory.java:53)
at com.fasterxml.jackson.databind.SerializerProvider._createUntypedSerializer(SerializerProvider.java:935)
at com.fasterxml.jackson.databind.SerializerProvider._createAndCacheUntypedSerializer(SerializerProvider.java:892)
at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:429)
at com.fasterxml.jackson.databind.SerializerProvider.findTypedValueSerializer(SerializerProvider.java:520)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:99)
at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:457)
at com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:583)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.writeMessageBody(JAXRSUtils.java:1173)
at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.serializeMessage(JAXRSOutInterceptor.java:259)
at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.processResponse(JAXRSOutInterceptor.java:155)
at org.apache.cxf.jaxrs.interceptor.JAXRSOutInterceptor.handleMessage(JAXRSOutInterceptor.java:86)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
at org.apache.cxf.interceptor.OutgoingChainInterceptor.handleMessage(OutgoingChainInterceptor.java:77)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:238)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:222)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:163)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:137)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:158)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:239)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:164)

问题出在这行代码中

com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector._renameWithWrappers(POJOPropertiesCollector.java:728) 

其中 _annotationIntrospector 为空

PropertyName wrapperName = _annotationIntrospector.findWrapperName(member);

不太疼我应该怎么做才能实现我的目标。有什么建议吗?谢谢。

4

1 回答 1

3

使用 mixins 为您的用例提供解决方案的小示例:

class Data {
    int totalUsers;
    int totalBooks;
}

…</p>

class DataMixin {
    @JsonProperty("Total Users")
    int totalUsers;
    @JsonProperty("Total Books")
    int totalBooks;
}

…</p>

Jackson 2.0 - 2.4(在 2.5 中已弃用)

ObjectMapper mapper = new ObjectMapper();
if (pretty) {
    mapper.addMixInAnnotations(Data.class, DataMixin.class);
}

杰克逊 2.5+

ObjectMapper mapper = new ObjectMapper();
if (pretty) {
    mapper.addMixin(Data.class, DataMixin.class);
}
于 2013-03-08T15:43:04.633 回答