spring boot基础之jackson

经常使用的json框架有阿里的fastjson 谷歌gson 等,从性能上来讲 javabean序列化为json  :   jackson > fastjson > gson > json-libjava

这里介绍几种jackson经常使用的一些注解:web

指定字段不返回:@JsonIgnorespring

指定日期格式:@JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh",timezone="GMT+8")json

空字段不返回:@JsonInclude(JsonInclude.Include.NON_NULL)app

指定别名:@JsonProperty("othername")框架

1.首先写个实体类:性能

 1 package com.aytsh.demostream;
 2 
 3 import java.util.Date;
 4 
 5 public class User {
 6 
 7     
 8     private String name;
 9 
10 
11     private int age;
12 
13   
14     private String phone;
15 
16    
17     private Date birthDay;
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public int getAge() {
28         return age;
29     }
30 
31     public void setAge(int age) {
32         this.age = age;
33     }
34 
35     public String getPhone() {
36         return phone;
37     }
38 
39     public void setPhone(String phone) {
40         this.phone = phone;
41     }
42 
43     public Date getBirthDay() {
44         return birthDay;
45     }
46 
47     public void setBirthDay(Date birthDay) {
48         this.birthDay = birthDay;
49     }
50 
51     public User(String name, int age, String phone, Date birthDay) {
52         this.name = name;
53         this.age = age;
54         this.phone = phone;
55         this.birthDay = birthDay;
56     }
57 }

而后,写个controller测试

 1 package com.aytsh.demostream.controller;
 2 
 3 import com.aytsh.demostream.User;
 4 import org.springframework.web.bind.annotation.GetMapping;
 5 import org.springframework.web.bind.annotation.RestController;
 6 
 7 import java.util.Date;
 8 
 9 @RestController
10 public class GetController {
11     
12     @GetMapping("/v2/test_json")
13     public Object testJson(){
14         return new User("hello",12,"119",new Date());
15     }
16 
17 }

作个测试先:this

如今但愿达到以下目的:spa

  1. 将“phone” 换个名字,好比 “mobile”
  2. 当 "name" 为null时,json中不显示
  3. 我不但愿别人知道个人年龄,所以我想在json中忽略
  4. 格式化个人日期格式

 

修改User类:

package com.aytsh.demostream;

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

import java.util.Date;

public class User{

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String name;


    @JsonIgnore
    private int age;

    @JsonProperty("mobile")
    private String phone;

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Date getBirthDay() {
        return birthDay;
    }

    public void setBirthDay(Date birthDay) {
        this.birthDay = birthDay;
    }

    public User(String name, int age, String phone, Date birthDay) {
       // this.name = name;
        this.age = age;
        this.phone = phone;
        this.birthDay = birthDay;
    }
}

从新测试:

相关文章
相关标签/搜索