比如我们有一个字段在数据库是datetime类型的,但是我们又需传给前端“yyyy-MM-dd”这样的格式,这时我们不需要再代码里面去手动用simpledateformat或者DateTimeFormatter去转,这样很转很麻烦的。
我们可以使用fasonjson的@JSONField注解,来format成任意我们想要的格式。
package com.alibaba.fastjson.annotation;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
public @interface JSONField {
@JSONField(name="test_date",format = "yyyy-MM-dd")
private Date testDate`;