SpringBoot2.x | Json及时间戳处理

pom.xml

<!-- Json解析 -->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.51</version
</dependency

Controller

/**
 * 会员列表路由
 * @return member-list.html
 */
@RequestMapping(value = "/member-list")
public String memberList(Model model,
                         @RequestParam(value = "page", defaultValue = "1") Integer page,
                         @RequestParam(value = "size", defaultValue = "10") Integer size) {
    // 获取会员信息
    List<Vip> vips = vipService.getAll(page, size);
    // 获取会员总人数
    int count = vipService.countById();
    // Json
    JSONObject obj = new JSONObject();
    obj.put("vips", vips);
    obj.put("count", count);
    model.addAttribute("obj", obj);
    return "member/member-list";
}

通过JsonFormat注解来处理Json返回的时间戳格式问题。

实体层entity

@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
private Date vipBirthday;

@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss", timezone = "GMT+8")
private Date vipCreateTime;

SpringBoot2.x | Json及时间戳处理