如何disable/enable杰克逊SerializationFeature.WRAP_ROOT_VALUE?

How to disable/enable jackson SerializationFeature.WRAP_ROOT_VALUE?

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

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

有办法动态地或从 class 本身禁用此功能吗?,

我试试这个:

致 wrap/unwrap 我这样做:

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 禁用该功能,这可能吗?

谢谢。

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