springboot全局日期格式化注意点

it2023-05-05  71

springboot全局日期格式化注意点

参考网址:

https://mp.weixin.qq.com/s?__biz=MzAwMjk5Mjk3Mw==&mid=2247492847&idx=2&sn=29b953cf808bd9dd27a3c7835839d4b0&chksm=9ac35acdadb4d3db8c9cf539212e09fed4dee6589bffa21805847790a2eb0d165b744e4a885b&mpshare=1&scene=23&srcid=1020R0WYini1qPGTA79oWs9C&sharer_sharetime=1603176255925&sharer_shareid=9d1e76e919cc0b2f3ca23ed1f5ef67a8#rd

发现问题

测试的controller类

package com.shaoming.controller; import com.shaoming.pojo.entity.User; import org.springframework.web.bind.annotation.*; import java.util.Date; /** * @Auther: shaoming * @Date: 2020/10/20 14:51 * @Description: */ @RestController @RequestMapping("/testdate") public class HelloController { @GetMapping("/getjson") public User userinfo() { return new User("1", "socks", "123456", new Date(), "GMT"); } @PostMapping("/post") public User getUserInfoByPost(@RequestBody User user) { return user; } @GetMapping("/get") public User getUserInfoByGet(User user) { return user; } @PostMapping("/postkeyvlaue") public User getUserInfoByPostKeyValue(User user) { return user; } }

每个方法对应的地址和参数

说明:

post请求我使用的工具是postman请求测试工具,很好用的一款测试接口的工具

1.直接返回json数据

方法

@GetMapping("/getjson") public User userinfo() { return new User("1", "socks", "123456", new Date(), "GMT"); }

url和参数

url http://localhost:8080/testdate/getjson 参数 无

2.post请求,参数为json格式

方法

@PostMapping("/post") public User getUserInfoByPost(@RequestBody User user) { return user; }

url和参数

url http://localhost:8080/testdate/post 参数 { "userId": "1", "username": "socks", "password": "123456", "createTime": "2020-10-20 15:06:12", "timezone": "GMT" }

3.get请求,参数拼接在url后面

方法

@GetMapping("/get") public User getUserInfoByGet(User user) { return user; }

url和参数

http://localhost:8080/testdate/get?userId=1&username=socks&password=123456&createTime=2020-10-20 15:06:12&timezone=GMT

4.post请求,参数是key=value形式

方法

@PostMapping("/postkeyvlaue") public User getUserInfoByPostKeyValue(User user) { return user; }

url和参数

http://localhost:8080/testdate/postkeyvlaue?userId=1&username=socks&password=123456&createTime=2020-10-20 15:06:12&timezone=GMT

说明:

第3个和第4个不是通过json格式传递数据,所以配置不起作用,还是要使用时间格式注解@DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)

`

`

其实全局时间配置类只是让我们不用写@JsonFormat(pattern = “yyyy/MM/dd HH:mm:ss”, timezone = “GMT+8”)这个注解

参考:

@Data public class DemoTime { private String name; @JsonProperty("date的值") @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date date; }

修改过后的user类

package com.shaoming.pojo.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import org.springframework.format.annotation.DateTimeFormat; import java.util.Date; /** * @Auther: shaoming * @Date: 2020/10/20 14:50 * @Description: */ @Data @AllArgsConstructor @NoArgsConstructor @Accessors(chain=true)//链式加载 public class User { private String userId; private String username; private String password; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date createTime; private String timezone; }

结论

这个工具类只是解决了传递json格式数据时间格式的问题

如果是key=value给实体类赋值的方式,那么还要使用注解@DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)

最新回复(0)