Jackson常用知识点和易错点

Maven配置:

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.5</version>
        </dependency>

大家也可以参照mvn官网查看。

 

这里使用一个User类来测试:

public class User {

    private int id;
    private String username;
    private String password;
    private String address;
    private Date birthday;
    private String phonenumber;
    public User(){}

    public User(int id, String username, String password, String address, Date birthday, String phonenumber) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.address = address;
        this.birthday = birthday;
        this.phonenumber = phonenumber;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getPhonenumber() {
        return phonenumber;
    }

    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", address='" + address + '\'' +
                ", birthday=" + birthday +
                ", phonenumber='" + phonenumber + '\'' +
                '}';
    }
}

在springboot配置相应的控制器:
 

@RestController
public class UserController {

    @GetMapping(value = "/user")
    public User getUser(){
        User user=new User();
        user.setId(1);
        user.setBirthday(new Date());
        user.setPassword("123");
        user.setPhonenumber("123456789");
        user.setUsername("git");
        user.setAddress("shanghai");
        return user;
    }
}

@RestController 相当于 @Controller + @ResponseBody 会把返回的内容以json的形式输出在页面上。

Jackson常用知识点和易错点

以上是默认输出的样式。

 

 jackson常用注解:

  • @JsonProperty(value="")    属性重命名

@JsonProperty(value = "my_id")
private int id;

Jackson常用知识点和易错点

这里原来的id属性名就变为了my_id。

  •  @JsonIgnore   忽略某属性

@JsonIgnore  
private String password;

Jackson常用知识点和易错点

password字段就不会再显示了。

  •  @JsonIgnoreProperties(value={})    忽略某些属性

当需要忽略多个属性时就可以使用该注解一次性忽略多属性。

@JsonIgnoreProperties(value = {"id", "username"})
public class User {

Jackson常用知识点和易错点

  • @JsonFormat(pattern="")   格式化属性

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")    
private Date birthday;

Jackson常用知识点和易错点

上一篇:mysql按时间查询(年/月/日)


下一篇:sparksql sql 对应关系