springboot 全局 Date参数接收 String格式 转换异常报错

 报错:

 .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.Date` from String "2022-12-13 11:22:11": not a valid representation (error: Failed to parse Date value '2022-12-13 11:22:11': Cannot parse date "2022-12-13 11:22:11": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null)); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2022-12-13 11:22:11": not a valid representation (error: Failed to parse Date value '2022-12-13 11:22:11': Cannot parse date "2022-12-13 11:22:11": while it seems to fit format 'yyyy-MM-dd'T'HH:mm:ss.SSSX', parsing fails (leniency? null))<EOL> at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 4, column: 14] (through reference chain: com.example.doatest.pojo.Car["createDate"])]
 

原因:
json序列化 类没对上, 参数接收用的Date ,传参拿到是 String 。需要做个转换。
 

场景:
接收参数是 Date类型 


 

 

 传参是 String格式 :


这个其实已经是很久很久很久之前就已经做过解决方案介绍了:
 

Springboot 全局日期格式化,只需要几行小代码_小目标青年的博客-CSDN博客_spring 日期格式

我还是推荐 yml配置的方案 :

#时区,默认为格林尼治时间,即少8小时,所以我们需要+8
spring.jackson.time-zone=GMT+8
#时间格式转换定义
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss

 轻松搞掂:

可能有人就是执拗啊,就是不想写配置,就是喜欢写代码, 然后又要全局生效转换,那怎么办?

也很好办,复制粘贴代码送给你:
 

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Author: JCccc
 * @Date: 2022-4-11 18:45
 * @Description:
 */
@Configuration
public class MyWebMvcConfigurer implements WebMvcConfigurer {

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = jackson2HttpMessageConverter.getObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        SimpleModule simpleModule = new SimpleModule();
        objectMapper.registerModule(simpleModule);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(0, jackson2HttpMessageConverter);
    }
    
}

直接把这个类丢到你的项目里面:

 轻松解决:

Spring Boot 是一个快速构建Spring应用程序的开发框架。在Spring Boot中,可以轻松地使用注解和配置来进行日期格式化。全局日期格式化可以确保应用程序中的所有日期在显示和解析时都遵循相同的格式。 要实现全局日期格式化,首先需要创建一个自定义的日期格式化器。可以通过创建一个继承自 WebMvcConfigurationSupport 的类,并覆盖其方法来实现自定义日期格式化器。在该类中,可以调用 setDateFormatter 方法来设置日期格式化器,将日期格式化模式作为参数传递给自定义日期格式化器。 例如,假设想在应用程序中将日期格式化为"yyyy-MM-dd"格式。可以创建一个 CustomDateFormatter 类,实现 DateFormatter 接口,并在实现类的 toFormatter 方法中设置日期格式化模式为"yyyy-MM-dd"。然后,在自定义的 WebMvcConfigurationSupport 类中,覆盖其 configureMessageConverters 方法,并在该方法中调用 setDateFormatter 方法来设置日期格式化器为 CustomDateFormatter。 在应用程序中任何地方,只需要使用 @DateTimeFormat 注解,并指定日期格式即可将日期解析为指定格式。同样地,使用该注解在返回对象上,可以将日期字段格式化为指定格式。 总而言之,Spring Boot 提供了全局日期格式化的机制,使得应用程序中的日期在显示和解析时都能遵循统一的格式。通过自定义日期格式化器,可以轻松地实现指定的日期格式,并在应用程序中使用 @DateTimeFormat 注解来格式化和解析日期。这样,可以确保应用程序中的日期始终遵循相同的格式要求。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小目标青年

对你有帮助的话,谢谢你的打赏。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值