Updated at 2022-06-06 10:03

可以借助Jackson的注解@JsonProperty的access属性实现。

  1. “跳过序列化”
public class DemoPojo1 {

    @JsonProperty(access = Access.WRITE_ONLY)
    private Long id;

    private String name;

    // getters, setters ...
}

Access.WRITE_ONLY代表只写,通过禁用getter实现,Jackson序列化时不再能访问到被声明属性,实现了该属性跳过序列化。

[^Access.WRITE_ONLY]: Access setting that means that the property may only be written (set) for deserialization, but will not be read (get) on serialization, that is, the value of the propertyis not included in serialization.

  1. “跳过序列化”
public class DemoPojo2 {

    private Long id;

    @JsonProperty(access = Access.READ_ONLY)
    private String name;

    // getters, setters ...
}

与Access.READ_ONLY同理。

[^Access.READ_ONLY]: Access setting that means that the property may only be read for serialization, but not written (set) during deserialization.


Java      Jackson