长风秋雁
不知道,看起来像transient属性修饰符加上PROPAGATE_TRANSIENT_MARKER就可以了。反序列化是直接的。不过,在序列化过程中,瞬态标记有些麻烦。您需要为字段提供 getter,否则您将遇到异常“无法识别的字段...未标记为可忽略”。为了满足额外的要求,我添加了一个使用 jackson mixins 的版本。请注意,mixin 版本生成带有空密码字段的 json,而 PROPAGATE_TRANSIENT_MARKER 版本根本不生成任何字段。PROPAGATE_TRANSIENT_MARKER 版本public class SerializeDeserializeAttributes { private final ObjectMapper mapper = new ObjectMapper();public <T> T fromJson(String json, Class<T> c) throws IOException { synchronized (mapper) { mapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, false); return mapper.readValue(json, c); }}public String toJson(Object o) throws JsonProcessingException { synchronized (mapper) { mapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true); return mapper.writeValueAsString(o); }} private static final String jsonFull = "{\"name\":\"A\",\"email\":\"a@a\",\"password\":\"a\",\"width\":1,\"height\":1}"; private static final String jsonPartial = "{\"name\":\"A\",\"email\":\"a@a\",\"width\":1,\"height\":1}"; private static final User user = new User("A", "a@a", "a", 1, 1); @Test public void serializeDeserialize() throws IOException { assertEquals(user, fromJson(jsonFull, User.class)); assertEquals(jsonPartial, toJson(user)); assertEquals(user, fromJson(jsonFull, User.class)); assertEquals(jsonPartial, toJson(user)); }}Mixin versionpublic class SerializeDeserializeAttributesMixin { public abstract class UserMixin { @JsonSerialize(using = PwdSerializer.class) transient String password; } static class PwdSerializer extends StdSerializer<String> { public PwdSerializer() { this(String.class); } private PwdSerializer(Class<String> t) { super(t); } @Override public void serialize(String s, JsonGenerator jg, SerializerProvider sp) throws IOException { jg.writeString(""); } } private static final String jsonFull = "{\"name\":\"A\",\"email\":\"a@a\",\"password\":\"a\",\"width\":1,\"height\":1}"; private static final String jsonPartialMixin = "{\"name\":\"A\",\"email\":\"a@a\",\"password\":\"\",\"width\":1,\"height\":1}"; private static final User user = new User("A", "a@a", "a", 1, 1); private static final ObjectMapper mapperMixin = new ObjectMapper(); static { mapperMixin.addMixIn(User.class, UserMixin.class); } @Test public void serializeDeserializeUsingMixin() throws IOException { assertEquals(user, mapperMixin.readValue(jsonFull, User.class)); assertEquals(jsonPartialMixin, mapperMixin.writeValueAsString(user)); }}那是用户类。class User { private String name; private String email; private transient String password; private int width; private int height; public User() { } User(String name, String email, String password, int width, int height) { this.name = name; this.email = email; this.password = password; this.width = width; this.height = height; } public String getName() { return name; } public String getEmail() { return email; } public String getPassword() { return password; } public int getWidth() { return width; } public int getHeight() { return height; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; return Float.compare(user.width, width) == 0 && Float.compare(user.height, height) == 0 && Objects.equals(name, user.name) && Objects.equals(email, user.email) && Objects.equals(password, user.password); } @Override public int hashCode() { return Objects.hash(name, email, password, width, height); } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", email='" + email + '\'' + ", password='" + password + '\'' + ", width=" + width + ", height=" + height + '}'; }}