Common usage of Jackson in the project

Posted by sanand158 on Mon, 20 Jan 2020 16:32:09 +0100

It is mainly used when the model object outputs and renders json. We can control the output results through related annotations, such as date format, non empty, sensitive data filtering, etc.
Here is a very simple project built with spring boot.

package com.hbk.springbootmail.controller;

import com.hbk.springbootmail.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/user")
public class UserController {
    @GetMapping("/hello")
    public User hello(){
        User user = new User();
        user.setId(1);
        user.setBirthday(new Date());
        user.setName("huangbaokang");
        user.setPassword("123456");
        return user;
    }
}

The model object provides get and set methods

package com.hbk.springbootmail.model;

import java.util.Date;

public class User {
    private Integer id;
    private String name;
    private String password;
    private Date birthday;
    private String desc;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

    public Date getBirthday() {
        return birthday;
    }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

The browser accesses localhost:8080/user/hello and gets the following results:

{"id":1,"name":"huangbaokang","password":"123456","birthday":"2020-01-20T06:21:51.396+0000","desc":null}

The password is a sensitive field, which needs to be filtered, and the time display format is incorrect, and desc is empty.

Set the following annotation and access the output as follows:

{"id":1,"name":"huangbaokang","birthday":"2020-01-20 02:31:40 Afternoon"}
package com.hbk.springbootmail.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import java.util.Date;

public class User {
    private Integer id;
    private String name;

    @JsonIgnore
    private String password;

    @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss a",locale="zh",timezone="GMT+8")
    private Date birthday;

    @JsonInclude(Include.NON_NULL)
    private String desc;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

    public Date getBirthday() {
        return birthday;
    }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}

This knowledge point tells us that it is convenient to set json format conversion at the back end when debugging the interface.

1158 original articles published, 252 praised, 1.91 million visitors+
His message board follow

Topics: Java JSON Spring