About the difference between @ DateTimeFormat, @ JsonFormat and @ JsonFiled when formatting date objects!

I am a program ape who has just joined the company. Under the guidance of my boss, I hereby publish an article to record the problems I encounter in my study and work. From this week, it will be updated every week. Welcome to discuss and study together! Don't say much, go straight to the subject.

@DateTimeFormat

Its function is to convert the string in the specified format into a date object when the front end returns to the back end for a request.

Define a student class entity

@Data
@AllArgsConstructor
public class StudentVo {
    private String name;

    private Integer age;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date time;
}

Test class

@Slf4j
@RestController
public class JsonTest {

    @PostMapping("/demo/1")
    public StudentVo getStudentJson1(@RequestBody StudentVo studentVo) {
        log.info("The request parameters are:" + studentVo);
        return studentVo;
    }

    @PostMapping("/demo/2")
    public String getStudentJson2(@RequestBody StudentVo studentVo) throws JsonProcessingException {
        log.info("The request parameters are:" + studentVo);
        ObjectMapper mapper = new ObjectMapper();//Convert to jackjason
        String jackJson = mapper.writeValueAsString(studentVo);
        log.info(jackJson);
        return jackJson;
    }

    @PostMapping("/demo/3")
    public String getStudentJson3(@RequestBody StudentVo studentVo) {
        log.info("The request parameters are:" + studentVo);
        String fastJson = JSON.toJSONString(studentVo);//Convert to fastjason
        log.info(fastJson);
        return fastJson;
    }
}

test result

 @JsonFormat

This annotation is a little more powerful than @ DateTimeFormat. It can not only parse the front-end string into the back-end Date object, but also convert the back-end date format into the front-end string. In addition, when using this annotation, you can set the attribute Timezone = "GMT+8" to East Zone 8.

Entity class

@Data
@AllArgsConstructor
public class StudentVo implements Serializable {
    private String name;

    private Integer age;

    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date time;
}

Test results

 @JsonFiled

JsonFiled is an annotation of fastjason, an open source of Alibaba. Like @ JsonFormat, it can not only parse the front-end string into the back-end Date object, but also convert the back-end date format into the front-end string. One feature is that it does not need to set the time zone, and it will be automatically set as the East 8 zone for us. In addition, it supports multiple type conversions other than date types. Here we only focus on the conversion of date format.

Replace the annotation on the entity with JsonFiled

@Data
@AllArgsConstructor
public class StudentVo implements Serializable {
    private String name;

    private Integer age;


    @JSONField(format = "yyyy-MM-dd")
    private Date time;
}

/ / test results (test results in demo3 above)

The above is the difference between the use of the three annotations. Next, Jack Jason and fastjason cannot be used with each other

@PostMapping("/demo/2")
    public String getStudentJson2(@RequestBody StudentVo studentVo) throws JsonProcessingException {
        log.info("The request parameters are:" + studentVo);
        ObjectMapper mapper = new ObjectMapper();//Convert to jackjason
        String jackJson = mapper.writeValueAsString(studentVo);
        log.info(jackJson);
        return jackJson;
    }

    @PostMapping("/demo/3")
    public String getStudentJson3(@RequestBody StudentVo studentVo) {
        log.info("The request parameters are:" + studentVo);
        String fastJson = JSON.toJSONString(studentVo);//Convert to fastjason
        log.info(fastJson);
        return fastJson;
    }

We can convert the StudentVo object annotated with @ JsonField to Jackson format output, and the result becomes like this

Test demo3 results

Then, on the contrary, convert the StudentVo object annotated with @ JsonFormat to fastJsong format, and the output result is the same

Test demo2 results

The above is my summary of the differences and precautions in the use of the three annotations. You are welcome to correct anything wrong!

Keywords: Java Spring

Added by veridicus on Mon, 20 Dec 2021 17:23:46 +0200