GSON相当于jackson的@JsonIgnoreProperties

在Jackson中,您可以通过在类级别提供注释@JsonIgnoreProperties来忽略属性,并且不在Java类中序列化/反序列化不在实际JSON中的属性。 如果我们使用GSON,它的等价物是什么?

使用GsonBuilder.excludeFieldsWithoutExposeAnnotation()使用GsonBuilder.excludeFieldsWithoutExposeAnnotation() @Expose注释获得类似的效果。

例如

  public class User { @Expose private String firstName; @Expose(serialize = false) private String lastName; @Expose (serialize = false, deserialize = false) private String emailAddress; private String password; } 

如果你使用Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create()与上面的类,那么toJson()fromJson()方法将完全忽略密码字段,因为它没有@Expose注释。

(注意,您还可以在此处获得更精细的控制,因为您可以控制GSON是否序列化/反序列化字段)。

参考: https : //github.com/google/gson/blob/master/UserGuide.md#TOC-Gson-s-Expose