@DateTimeFormat 转换报错

251 天前
 cencoroll

今天碰到一个情况就是 get 请求到后端的时候日期无法转换,但是 Date 上明明使用了注解但是就是报错

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

报错信息是这样的

code:400
data:{}
msg: 
"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.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 就不会报错。 请教一下大佬到底怎么办才能让后端正确接收这个不带时分秒的日期?

1261 次点击
所在节点    Java
19 条回复
xianyv
251 天前
为啥你的 DateTimeFormat 的包名是 com.alibaba 的?不是 org.springframework.format.annotation 的吗
missz
251 天前
2023-9-01 换成 2023-09-01 试试
cencoroll
251 天前
@missz 都试过了,2023-9-1 2023-09-01 2023-9-01 这三个一样都是报错
@xianyv 这两个报名一样报错,都试过了,排查了一上午了
cencoroll
251 天前
@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]
```
TinyBad
251 天前
用 LocalDate
oldshensheep
251 天前
2023 年了还有人在用 java.util.Date
换成 java.time.Instant
Huelse
251 天前
java.time.LocalDate
wengyanbin
251 天前
本地跑了下,没有报错。
@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());
}
}
cencoroll
251 天前
@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;
```
wengyanbin
250 天前
@cencoroll 这个看着应该是参数转化器的问题。接受参数用的是哪个注解?
jamezee
250 天前
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;

我这的项目里就这么用的,没问题,可能是版本或者配置的问题
hrapunzel
250 天前
前端传的参数是什么样的
wengyanbin
250 天前
@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 ,这是请求的路径,可以转化没有报错。
cslive
250 天前
把你 fastjson 换成 jackson 试试,为啥改默认的
cencoroll
250 天前
@hrapunzel 9 楼那贴出来了,@GetMapping 用 url 传的参数。list?queryType=TEAM&date=2023-9-01 (这里的不管是 09-01 还是 9-1 样都是报错)
@cslive fastjson 和 jackson 两个都试过,一样都是报错
xianyv
250 天前
切面或者拦截器看一下自己到底接收了什么东西
kraken9527
250 天前
看看 class 文件是这个内容嘛,实在不行加个断点 debug 一下
Naccl
250 天前
看看项目配置文件里,有没有这类冲突的配置
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
Naccl
250 天前
或者看下有没有注入过 Date Converter 的 bean

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/970064

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX