Use class name as root key for JSON Jackson serialization

前端 未结 11 2797
长情又很酷
长情又很酷 2020-11-27 18:05

Suppose I have a pojo:

import org.codehaus.jackson.map.*;

public class MyPojo {
    int id;
    public int getId()
    { return this.id; }

    public void          


        
11条回答
  •  我在风中等你
    2020-11-27 18:36

    I'm not using jackson, but searching I found this configuration that seems to be what you want: WRAP_ROOT_VALUE

    Feature that can be enabled to make root value (usually JSON Object but can be any type) wrapped within a single property JSON object, where key as the "root name", as determined by annotation introspector (esp. for JAXB that uses @XmlRootElement.name) or fallback (non-qualified class name). Feature is mostly intended for JAXB compatibility.

    Default setting is false, meaning root value is not wrapped.

    So that you can configure mapper:

    objectMapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
    

    I hope it helps you...

提交回复
热议问题