今天碰到一个情况就是 get 请求到后端的时候日期无法转换,但是 Date 上明明使用了注解但是就是报错
@DateTimeFormat(pattern = "yyyy-MM-dd") //@JsonFormat(pattern = "yyyy-MM-dd" , timezOne="GMT+8") private Date date;
报错信息是这样的
code:400 data:{} msg: "date:Failed t convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.alibaba.excel.annotation.format.DateTimeFormat java.util.Date] for value '2023-9-01'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2023-9-01]" success:false
迷惑的点在于如果加上 HH:mm:ss 的话,把日期+00:00:00 就不会报错。 请教一下大佬到底怎么办才能让后端正确接收这个不带时分秒的日期?
![]() | 1 xianyv 2023-09-01 13:17:49 +08:00 为啥你的 DateTimeFormat 的包名是 com.alibaba 的?不是 org.springframework.format.annotation 的吗 |
2 missz 2023-09-01 13:19:24 +08:00 2023-9-01 换成 2023-09-01 试试 |
3 KamL OP |
4 KamL OP @cencoroll 报名-->包名,基本把能排查的都排查了一遍了,就是说转换异常 ``` date:Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2023-09-01'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2023-09-01] ``` |
![]() | 5 TinyBad 2023-09-01 13:37:04 +08:00 用 LocalDate |
![]() | 6 oldshensheep 2023-09-01 13:41:26 +08:00 2023 了还有人在用 java.util.Date 换成 java.time.Instant |
7 Huelse 2023-09-01 13:51:35 +08:00 java.time.LocalDate |
8 wengyanbin 2023-09-01 14:08:07 +08:00 本地跑了下,没有报错。 @Getter @Setter public class UserModel { @DateTimeFormat(pattern = "yyyy-MM-dd") //@JsonFormat(pattern = "yyyy-MM-dd") private Date birthday; public static void main(String[] args) throws JsonProcessingException { String a="{\"birthday\":\"2010-01-01\"}"; ObjectMapper objectMapper=new ObjectMapper(); UserModel user = objectMapper.readValue(a, UserModel.class); System.out.println(user.getBirthday()); } } |
9 KamL OP @wengyanbin 头都大了,如果带时分秒就能转换成功。但是不带的话一定报错 ``` list?queryType=TEAM&date=2023-9-01+00:00:00 ``` 像这样就完全没问题 ``` @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd" , timezOne="GMT+8") private Date date; ``` |
10 wengyanbin 2023-09-01 14:50:58 +08:00 @cencoroll 这个看着应该是参数转化器的问题。接受参数用的是哪个注解? |
11 jamezee 2023-09-01 14:57:05 +08:00 import com.fasterxml.jackson.annotation.JsonFormat; import org.springframework.format.annotation.DateTimeFormat; ... @DateTimeFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd" , timezOne="GMT+8") private Date date; 我这的项目里就这么用的,没问题,可能是版本或者配置的问题 |
![]() | 12 hrapunzel 2023-09-01 15:00:27 +08:00 前端传的参数是什么样的 |
13 wengyanbin 2023-09-01 15:09:22 +08:00 @cencoroll @RestController @RequestMapping("/test") public class TestController { @GetMapping("/list") public ResponseEntity<String> list(UserModel userModel){ System.out.println(userModel.getBirthday()); return ResponseEntity.ok("success"); } } 我在 controller 里面这么用也是可以的,localhost:8082/test/list?birthday=2010-08-01 ,这是请求的路径,可以转化没有报错。 |
14 cslive 2023-09-01 15:18:51 +08:00 把你 fastjson 换成 jackson 试试,为啥改默认的 |
15 KamL OP |
![]() | 16 xianyv 2023-09-01 16:48:41 +08:00 切面或者拦截器看一下自己到底接收了什么东西 |
![]() | 17 kraken9527 2023-09-01 16:53:44 +08:00 via Android 看看 class 文件是这个内容嘛,实在不行加个断点 debug 一下 |
![]() | 18 Naccl 2023-09-01 17:15:20 +08:00 看看项目配置文件里,有没有这类冲突的配置 spring.jackson.date-format=yyyy-MM-dd HH:mm:ss |
![]() | 19 Naccl 2023-09-01 17:19:57 +08:00 或者看下有没有注入过 Date Converter 的 bean |