使用Lombok 和jackson序列化时的爆炸性问题

实体类如下:

@Data
public class JsonDto {

    private String cDay;
    private String ceDay;

    public JsonDto() {
    }
    public JsonDto(String cDay, String ceDay) {
        this.cDay = cDay;
        this.ceDay = ceDay;
    }
}


public class JsonDto1 {
    private String cDay;
    private String ceDay;

    public String getcDay() {
        return cDay;
    }

    public void setcDay(String cDay) {
        this.cDay = cDay;
    }

    public String getCeDay() {
        return ceDay;
    }

    public void setCeDay(String ceDay) {
        this.ceDay = ceDay;
    }

    public JsonDto1() {
    }
    public JsonDto1(String cDay, String ceDay) {
        this.cDay = cDay;
        this.ceDay = ceDay;
    }
}


@Data
public class JsonDto2 {
    @JsonProperty
    private String cDay;
    private String ceDay;

    public JsonDto2() {
    }
    public JsonDto2(String cDay, String ceDay) {
        this.cDay = cDay;
        this.ceDay = ceDay;
    }
}




import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * @author Reese
 */
public class JsonDto3 {
    @JsonProperty
    private String cDay;
    private String ceDay;

    public String getcDay() {
        return cDay;
    }

    public void setcDay(String cDay) {
        this.cDay = cDay;
    }

    public String getCeDay() {
        return ceDay;
    }

    public void setCeDay(String ceDay) {
        this.ceDay = ceDay;
    }

    public JsonDto3() {
    }
    public JsonDto3(String cDay, String ceDay) {
        this.cDay = cDay;
        this.ceDay = ceDay;
    }
}



public class JsonDto4 {

    private String cDay;
    private String ceDay;

    public String getCDay() {
        return cDay;
    }

    public void setCDay(String cDay) {
        this.cDay = cDay;
    }

    public String getCeDay() {
        return ceDay;
    }

    public void setCeDay(String ceDay) {
        this.ceDay = ceDay;
    }

    public JsonDto4() {
    }
    public JsonDto4(String cDay, String ceDay) {
        this.cDay = cDay;
        this.ceDay = ceDay;
    }
}


经过测试如下


@RestController
public class JsonDtoController {
    /**
     * 测试结果 {"ceDay":null,"cday":null}
     * @return
     */
    @GetMapping("/jd")
    public JsonDto get(){
        return new JsonDto();
    }

    /**
     * {"cDay":null,"ceDay":null}
     * @return
     */
    @GetMapping("/jd1")
    public JsonDto1 get1(){
        return new JsonDto1();
    }

    /**
     * {"cDay":null,"ceDay":null,"cday":null}
     * 会多出来一个属性
     * @return
     */
    @GetMapping("/jd2")
    public JsonDto2 get2(){
        return new JsonDto2();
    }

    /**
     * {"cDay":null,"ceDay":null}
     * @return
     */
    @GetMapping("/jd3")
    public JsonDto3 get3(){
        return new JsonDto3();
    }

    /**
     * {"ceDay":"BB","cday":"aa"}
     * @return
     */
    @GetMapping("/jdc")
    public JsonDto getc(){
        return new JsonDto("aa","BB");
    }

    /**
     * {"ceDay":"BB","cDay":"aa"}
     * @return
     */
    @GetMapping("/jd1c")
    public JsonDto1 get1c(){
        return new JsonDto1("aa","BB");
    }

    /**
     * {"cDay":"aa","ceDay":"BB","cday":"aa"}
     * @return
     */
    @GetMapping("/jd2c")
    public JsonDto2 get2c(){
        return new JsonDto2("aa","BB");
    }

    /**
     * {"ceDay":"BB","cDay":"aa"}
     * @return
     */
    @GetMapping("/jd3c")
    public JsonDto3 get3c(){
        return new JsonDto3("aa","BB");
    }

    /**
     * {"ceDay":"BB","cDay":"aa"}
     * @return
     */
    @GetMapping("/jd4")
    public JsonDto4 get4(){
        return new JsonDto4("aa","BB");
    }/**
     * {"ceDay":"BB","cDay":"aa"}
     * @return
     */
    @GetMapping("/jd4c")
    public JsonDto4 get4c(){
        return new JsonDto4("aa","BB");
    }
}

使用的 jackson版本 2.9.5

lombok 版本 是 1.16.20

spring boot 项目


为什么 JsonDto2 会多出来一个属性值,这到底是谁的问题????

原文:https://zhuanlan.zhihu.com/p/37439920






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值