Json 扁平化对象 @JsonUnwrapped

@Data
public class Account {

    @JsonUnwrapped
    private Location location;
    @JsonUnwrapped
    private PersonInfo personInfo;

    @Data
    public static class Location {
        private String provinceName;
        private String countyName;
    }

    @Data
    public static class PersonInfo {
        private String userName;
        private String fullName;
    }

}

未扁平化之前:

{
    "location": {
        "provinceName":"广东",
        "countyName":"深圳"
    },
    "personInfo": {
        "userName": "111",
        "fullName": "222"
    }
}

使用 @JsonUnwrapped 扁平对象之后:

{
  "provinceName":"广东",
  "countyName":"深圳",
  "userName": "111",
  "fullName": "222"
}

不同对象间有相同名称的属性?会怎么样,会报错吗?答案是不会,都会正常返回,只是对前端而言有歧义罢了~,此时可以借助 @JsonUnwrapped 的 suffix 和 prefix 来规避这个问题

Json 扁平化对象 @JsonUnwrapped_第1张图片
JsonUnwrapped

你可能感兴趣的:(Json 扁平化对象 @JsonUnwrapped)