【问题标题】:How to disable/enable jackson SerializationFeature.WRAP_ROOT_VALUE?如何禁用/启用杰克逊 SerializationFeature.WRAP_ROOT_VALUE?
【发布时间】:2016-01-03 12:14:05
【问题描述】:

我使用的是 JSONAPI,所以我需要包装一些类,但不是所有类,例如:

{"users": {"aKey": "aValue"}} // wrapped.
{"aKey": "aValue"} // not wrapped.

有一种方法可以动态或从类本身禁用 tis 功能?,

我试试这个:

包装/打开包装我这样做:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
objectMapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

JacksonConverterFactory jacksonConverterFactory = JacksonConverterFactory.create(objectMapper);

OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.interceptors().add(new LoggingInterceptor());

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(okHttpClient)
            .addConverterFactory(jacksonConverterFactory)
            .build();

我需要一些 POJO 禁用该功能,这可能吗?

谢谢。

【问题讨论】:

  • 你找到方法了吗?
  • 我已经为此here提出了解决方法

标签: java android jackson retrofit


【解决方案1】:

目前,没有。这是在FasterXML/jackson-databind#1022 下跟踪的 作为一种解决方法,您可以创建两个不同的改造实例,一个带有启用了 root 的转换器工厂,一个没有。

【讨论】:

    猜你喜欢
    • 2016-10-19
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多