导读
由于项目需要,使用Lombok的时候 @JsonFormat
格式化日期的时候不起作用。
使用
实体类
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.util.Date;
@Data
public class Ts {
private String id;
private String name;
private Integer age;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date cTime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date insertime;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updateTime;
}
测试
控制层
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@RequestMapping("/api")
@RestController
public class TsController {
@RequestMapping("/index")
public List<Ts> execute() {
Ts ts = new Ts();
ts.setAge(10);
ts.setCTime(new Date());
ts.setInsertime(new Date());
ts.setUpdateTime(new Date());
ts.setId(UUID.randomUUID().toString());
ts.setName("小米");
Ts s = new Ts();
s.setAge(null);
s.setCTime(new Date());
s.setInsertime(new Date());
s.setUpdateTime(new Date());
s.setId(String.valueOf(Math.random()));
s.setName("小吴");
List list = new ArrayList<>();
list.add(s);
list.add(ts);
return list;
}
}
发送请求获取数据
GET : http://localhost:8080/api/index
[
{
"id": "0.8696139330472303",
"name": "小吴",
"cTime": "2020-10-27 10:28:09",
"insertime": "2020-10-27 10:28:09",
"updateTime": "2020-10-27 10:28:09",
"ctime": "2020-10-27T02:28:09.045+0000"
},
{
"id": "39547d17-d214-4fee-8b07-eb39e8e6edb3",
"name": "小米",
"age": 10,
"cTime": "2020-10-27 10:28:09",
"insertime": "2020-10-27 10:28:09",
"updateTime": "2020-10-27 10:28:09",
"ctime": "2020-10-27T02:28:09.045+0000"
},
{
"id": "10",
"address": "杭"
}
]
结论
使用Lombok的定义的实体类的时候,属性的第二个字母不支持大写,否则会出现重复大小写结果(如上述cTime、和ctime两条数据)。支持除第二个字母之外的字母大写。
END
搞定~