Use of @JsonFormat and @DataFormat

 Notes @JsonFormat mainly background to foreground the time format conversion

  Notes @DateTimeFormat mainly around to the back of the time format conversion

@DateTimeFormat and date information may be @JsonFormat between JSON format conversion objects and java.util.Date

@DateTimeFormat

This provides annotations for the Spring Framework, the date information JSON format conversion information analysis and bind to the Date object, the Date field can be used for annotation, specifying the date JSON format (pattern)

class Pc{    
        ...        
        private String name;
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        private Date birthday;
        ...
// 测试接收json字符串的日期信息 将其解析转换为 Date
    @RequestMapping("/demo1")
    public static void demo1(@RequestBody Pc pc){    
        System.out.println("Name: " + pc.getName());
        System.out.println("Brithday: " + pc.getBirthday());
        return;
    }

8080 / demo1 sending a POST request, JSON data in the request body are as follows: to http: // localhost:

{
    "name": "Aaron",
    "birthday": "2001-06-08 11:22:33"
}

Console output can be observed as follows:

Name: Aaron
    Brithday: Fri Jun 08 00:00:00 CST 2001

JSON can be seen that the front end date data transmission is correctly resolve the rear end, and bind to the Type Date field, to be noted that, in accordance with the parsing parses the given JSON pattern. So, JSON, if a digit month and day, you need to fill the leading 0; and as for the latter HH: mm: ss due not write in pattern, it is not be resolved, it is set to 0

@JsonFormat

Jackson provided for annotation framework, the Date object into a JSON format data analysis, annotation for the Date field can also specify a desired date JSON format (pattern)

class Pc{    
        ...        
        private String name;
        @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd") 
        private Date birthday;
        ...
import cn.hutool.core.date.DateTime;
    ...

    // 测试 将 Date日期数据 转换为 JSON格式数据
    @RequestMapping("/demo2")
    public static Pc demo2(@RequestBody Pc pc){
        pc.setBirthday(DateTime.now());
        System.out.println("Brithday: " + pc.getBirthday());
        System.out.println("Name: " + pc.getName());
        return pc;
    }

To http: // localhost: 8080 / demo2 sending a POST request, JSON data request body is:

{
    "name": "Tony",
    "birthday": null
}

Console output can be observed as follows:

Brithday: 2019-07-31 17:20:04
    Name: Tony

JSON body responds as follows:

{
        "name": "Tony",
        "birthday": "2019-07-31"
    }

Output from the console, Date data can be seen in the rear end is set correctly. Meanwhile Date object to parse the data is correctly written into the data and JSON response body. It should be noted that the resolution will be resolved to the given JSON JSON pattern. Therefore, the latter HH: mm: ss every minute when no information is written in pattern because, it is not parsed.

 

Published 272 original articles · won praise 19 · views 20000 +

Guess you like

Origin blog.csdn.net/hello_cmy/article/details/104823508