springboot LocalDateTime 反序列化
springboot LocalDateTime 反序列化
@RequestBody读取pojo中LocalDateTime属性字段,字符串格式为"yyyy-MM-dd HH:mm:ss"时反序列化失败
*******************
示例
***************
pojo 层
Order
@Data
public class Order {
private String orderId;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime orderTime;
private Double price;
private Integer amount;
}
***************
controller 层
HelloController
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime localDateTime){
System.out.println("hello ==> "+localDateTime);
return "success";
}
@RequestMapping("/hello2")
public String hello2(Order order){
System.out.println("hello2 ==> "+order);
return "success";
}
@RequestMapping("/hello3")
public String hello3(@RequestBody Order order){
System.out.println("hello3 ==> "+order);
return "success";
}
}
*******************
使用测试
***************
@RequestParam
localhost:8080/hello?localDateTime=2020-08-09 08:06:09
hello ==> 2020-08-09T08:06:09
localhost:8080/hello2?orderId=1&orderTime=2020-08-06 08:06:09&price=2&amount=8
hello2 ==> Order(orderId=1, orderTime=2020-08-06T08:06:09, price=2.0, amount=8)
@RequestParam可正常操作LocalDateTime数据
***************
@RequestBody
localhost:8080/hello3
header: Content-Type ==> application/json
body:
控制台输出
2020-07-16 17:33:54.416 WARN 10404 --- [nio-8080-exec-5]
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved
[org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error:
Cannot deserialize value of type `java.time.LocalDateTime` from String "2020-08-09
08:06:09": Failed to deserialize java.time.LocalDateTime:
(java.time.format.DateTimeParseException) Text '2020-08-09 08:06:09' could not be parsed at
index 10; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException:
Cannot deserialize value of type `java.time.LocalDateTime` from String "2020-08-09
08:06:09": Failed to deserialize java.time.LocalDateTime:
(java.time.format.DateTimeParseException) Text '2020-08-09 08:06:09' could not be parsed at
index 10
at [Source: (PushbackInputStream); line: 3, column: 18] (through reference chain:
com.example.demo.pojo.Order["orderTime"])]
localDateTime反序列化失败,不能将2020-08-09 08:06:09转换为localDateTime
*******************
解决措施
***************
引入jar 包
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.11.1</version>
</dependency>
***************
pojo 层
Order
@Data
public class Order {
private String orderId;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime orderTime;
private Double price;
private Integer amount;
}
使用@JsonFormat读取输入参数进行序列化
*******************
使用测试
localhost:8080/hello3
header: Content-Type ==> application/json
body:
控制台输出
hello3 ==> Order(orderId=2, orderTime=2020-08-09T08:06:09, price=2.0, amount=8)
本文地址:https://blog.csdn.net/weixin_43931625/article/details/107387865
上一篇: mac 安装使用jenkins
推荐阅读
-
SpringBoot中对LocalDateTime进行格式化
-
springboot~mybatis里localdatetime序列化问题
-
springboot 项目中获取默认注入的序列化对象 ObjectMapper
-
springboot LocalDateTime 反序列化
-
SpringBoot整合SpringMVC之自定义JSON序列化器和反序列化器
-
SpringBoot2.3整合redis缓存自定义序列化的实现
-
springboot反爬虫组件kk-anti-reptile的使用方法
-
springboot LocalDateTime 序列化、反序列化全局配置
-
Redission LocalDateTime序列化报错
-
gson反序列化localdateTime格式化