如何禁用/启用jackson SerializationFeature.WRAP_ROOT_VALUE?

neb*_*ros 8 java android jackson retrofit

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

{"users": {"aKey": "aValue"}} // wrapped.
{"aKey": "aValue"} // not wrapped.
Run Code Online (Sandbox Code Playgroud)

有一种方法可以动态地或从类本身禁用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();
Run Code Online (Sandbox Code Playgroud)

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

谢谢.

小智 2

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