Use class name as root key for JSON Jackson serialization

前端 未结 11 2793
长情又很酷
长情又很酷 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:35

    By adding the jackson annotation @JsonTypeInfo in class level you can have the expected output. i just added no-changes in your class.

    package com.test.jackson;
    
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.SerializationConfig;
    
    import com.fasterxml.jackson.annotation.JsonTypeInfo;
    import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
    import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
    
    @JsonTypeInfo(include=As.WRAPPER_OBJECT, use=Id.NAME)
    public class MyPojo {
        // Remain same as you have
    }
    

    output:

    {
        "MyPojo": {
            "id": 4
        }
    }
    

提交回复
热议问题