# @JsonFormat
作用:从数据库或者其它地方获取到时间但是通过 Controoler 返回的 json 格式时却是毫秒值的我们要想能看得懂需要进行格式化
database
+------------+---------+---------+------------+----+ | |
| potime | kecheng | xuefei | jiaofei | id | | |
+------------+---------+---------+------------+----+ | |
| 2023-04-17 | Java | 5099.89 | 2023-04-17 | 1 | | |
+------------+---------+---------+------------+----+ |
domain
@Getter | |
@Setter | |
@ToString | |
public class StudentXinxi { | |
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") // 出参 | |
private Date potime; | |
private String kecheng; | |
private Double xuefei; | |
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") // 出参 | |
private Date jiaofei; | |
private Integer id; | |
} |
controller
@Controller | |
public class StudentController { | |
@Autowired | |
private StudentService service; | |
@RequestMapping("/findAll") | |
@ResponseBody | |
public List<StudentXinxi> findAll(){ | |
return service.findAll(); | |
} | |
} |
请求浏览器后的结果
//before | |
[ | |
{ | |
"potime": 1681660800000, | |
"kecheng": "Java", | |
"xuefei": 5099.89, | |
"jiaofei": 1681660800000, | |
"id": 1 | |
} | |
] | |
//after | |
[ | |
{ | |
"potime": "2023-04-17 00:00:00", | |
"kecheng": "Java", | |
"xuefei": 5099.89, | |
"jiaofei": "2023-04-17 00:00:00", | |
"id": 1 | |
} | |
] |