使用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






你可能感兴趣的:(Java高级)