忽略基于另一个字段值的序列化字段

我有这样的课:


public class Profile {

        private String firstName;

        private String lastName;

        private String nickName;

        @JsonIgnore

        private boolean isNameSecret;

}

序列化对象时可以隐藏属性firstName,lastName如果isNameSecret值为true?


幕布斯6054654
浏览 75回答 2
2回答

SMILET

您需要编写自定义序列化程序并实现该逻辑。它可能如下所示:import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.databind.JsonSerializer;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.SerializationFeature;import com.fasterxml.jackson.databind.SerializerProvider;import com.fasterxml.jackson.databind.annotation.JsonSerialize;import java.io.IOException;public class ProfileApp {&nbsp; &nbsp; public static void main(String[] args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; &nbsp; &nbsp; mapper.enable(SerializationFeature.INDENT_OUTPUT);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(mapper.writeValueAsString(new Profile()));&nbsp; &nbsp; }}class ProfileJsonSerialize extends JsonSerializer<Profile> {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void serialize(Profile profile, JsonGenerator gen, SerializerProvider serializers) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; gen.writeStartObject();&nbsp; &nbsp; &nbsp; &nbsp; if (!profile.isNameSecret()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gen.writeStringField("firstName", profile.getFirstName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gen.writeStringField("lastName", profile.getLastName());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; gen.writeStringField("nickName", profile.getNickName());&nbsp; &nbsp; &nbsp; &nbsp; gen.writeEndObject();&nbsp; &nbsp; }}@JsonSerialize(using = ProfileJsonSerialize.class)class Profile {&nbsp; &nbsp; private String firstName = "Rick";&nbsp; &nbsp; private String lastName = "Sanchez";&nbsp; &nbsp; private String nickName = "Pickle Rick";&nbsp; &nbsp; private boolean isNameSecret = true;&nbsp; &nbsp; public String getFirstName() {&nbsp; &nbsp; &nbsp; &nbsp; return firstName;&nbsp; &nbsp; }&nbsp; &nbsp; public void setFirstName(String firstName) {&nbsp; &nbsp; &nbsp; &nbsp; this.firstName = firstName;&nbsp; &nbsp; }&nbsp; &nbsp; public String getLastName() {&nbsp; &nbsp; &nbsp; &nbsp; return lastName;&nbsp; &nbsp; }&nbsp; &nbsp; public void setLastName(String lastName) {&nbsp; &nbsp; &nbsp; &nbsp; this.lastName = lastName;&nbsp; &nbsp; }&nbsp; &nbsp; public String getNickName() {&nbsp; &nbsp; &nbsp; &nbsp; return nickName;&nbsp; &nbsp; }&nbsp; &nbsp; public void setNickName(String nickName) {&nbsp; &nbsp; &nbsp; &nbsp; this.nickName = nickName;&nbsp; &nbsp; }&nbsp; &nbsp; public boolean isNameSecret() {&nbsp; &nbsp; &nbsp; &nbsp; return isNameSecret;&nbsp; &nbsp; }&nbsp; &nbsp; public void setNameSecret(boolean nameSecret) {&nbsp; &nbsp; &nbsp; &nbsp; isNameSecret = nameSecret;&nbsp; &nbsp; }}上面的代码打印:{&nbsp; "nickName" : "Pickle Rick"}

慕容森

这可以使用 Jackson 的Mixin功能来实现。它允许在运行时应用来自外部类的注释。通过指定相同的属性名称来完成匹配(也适用于 getter/setter 等方法名称)这是根据问题(为简单起见将实例变量公开)的示例,请注意 mixin 类仅包含您要覆盖的属性。此外,它不需要初始化。具有相同属性名称并添加注释的 mixin 类public class ProfileIgnoreFirstLastName{&nbsp; &nbsp; @JsonIgnore&nbsp; &nbsp; public String firstName;&nbsp; &nbsp; @JsonIgnore&nbsp; &nbsp; public String lastName;}使用 mixin 类的条件应用进行序列化:public static String serializeProfile(Profile profile) throws IOException {&nbsp; &nbsp; ObjectMapper mapper = new ObjectMapper();&nbsp; &nbsp; if (profile.isNameSecret) {&nbsp; &nbsp; &nbsp; &nbsp; mapper.addMixIn(Profile.class, ProfileIgnoreFirstLastName.class);&nbsp; &nbsp; }&nbsp; &nbsp; return mapper.writeValueAsString(profile);}测试方法public static void main(String[] args) {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; Profile profile = new Profile();&nbsp; &nbsp; &nbsp; &nbsp; profile.isNameSecret = false;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(serializeProfile(profile));&nbsp; &nbsp; &nbsp; &nbsp; profile.isNameSecret = true;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(serializeProfile(profile));&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}输出{"firstName":"my first name","lastName":"my last name","nickName":"my nick name"}{"nickName":"my nick name"}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java