4.Jackson返回结果处理

jackson处理相关自动

1.指定字段不返回:@JsonIgnore
2.指定日期格式:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")
3.空字段不返回:@JsonInclude(Include.NON_NUll)
4.指定别名:@JsonProperty
示例:

public class User {
    private String sex;
    @JsonProperty("xm")//指定返回别名
    @JsonInclude(Include.NON_NULL)//空字段不返回
    private String name;
    @JsonIgnore//指定字段不返回
    private String phoneno;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",locale = "zh",timezone = "GMT+8")//指定返回日期格式:
    private Date birthday;
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPhoneno() {
        return phoneno;
    }
    public void setPhoneno(String phoneno) {
        this.phoneno = phoneno;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public User(String sex, String name, String phoneno, Date birthday) {
        super();
        this.sex = sex;
        this.name = name;
        this.phoneno = phoneno;
        this.birthday = birthday;
    }
    
}

测试

@RestController
public class SimpleTestController {
    Map map = new HashMap();
    @GetMapping("/testjson")
    public Object testJson() {
        return new User("女",null,"12356xxx",new Date());
    }
}

你可能感兴趣的:(4.Jackson返回结果处理)