我们将 REST API 编码成JSON格式,然后将它解码到POJO。 Jackson 的org.codehaus.jackson.map.ObjectMapper"只能"开箱即用,并且在大多数情况下我们并不能做任何其他事情。但有时我们确实需要一个定制的反序列化器以满足我们的定制需求,所以本教程将指导大家如何创建自定义的反序列化。 比方说,我们有以下实体: public class User { private Long id; private String name; private String email; public Long getId() { return id; } public User setId(Long id) { this.id = id; return this; } public String getName() { return name; } public User setName(String name) { this.name = name; return this; } public String getEmail() { return email; } public User setEmail(String email) { this.email = email; return this; } @Override public String toString() { final StringBuffer sb = new StringBuffer("User{"); sb.append("id=").append(id); sb.append(", name='").append(name).append('\''); sb.append(", email='").append(email).append('\''); sb.append('}'); return sb.toString(); } } public class Program { private Long id; private String name; private User createdBy; private String contents; public Program(Long id, String name, String contents,
Read full article from 用Jackson自定义JSON反序列化 �C 码农网
No comments:
Post a Comment