String转Date问题

报错信息

未能将‘ java.lang.string’类型的属性值转换为‘ po.endtime’属性所需的类型‘ java.util.date’; 
嵌套异常为 org.springwork.core.convert.convert.converversionfailedexception: 
未能将[ java.lang.string ]类型转换为[@org ]类型。
值“2022-03-30”的 
springframework.format.annotation.datetimformat java.util.date; 嵌套异常是 
java.lang.illegalargumentexception: 值的解析尝试失败[2022-03-30]
"JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "2022-02-28T16:12:00.000Z": Failed to deserialize java.time.LocalDateTime: 
(java.time.format.DateTimeParseException) Text '2022-02-28T16:12:00.000Z' 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 "2022-02-28T16:12:00.000Z": Failed to 
deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2022-02-28T16:12:00.000Z' could not be parsed at index 10
 at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 171] (through reference chain: 
 com.suzui.homework.entity.Homework["hbegindate"])"

加上@DateTimeFormat注解之后依然失败。

img

解决办法

使用fastjson替换springboot原本的jackson

添加依赖

image-20220330142723256

编写配置类

配置类中需要注意设置的日期格式
image-20220330142813064

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableWebMvc // 必须要有这个注解,否则报错
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        config.setCharset(StandardCharsets.UTF_8);
        config.setDateFormat("yyyy-MM-dd HH:mm:ssS");
        config.setSerializerFeatures(SerializerFeature.WriteMapNullValue);
        fastJsonConverter.setFastJsonConfig(config);
        List<MediaType> list = new ArrayList<>();
        list.add(MediaType.APPLICATION_JSON);
        fastJsonConverter.setSupportedMediaTypes(list);
        converters.add(fastJsonConverter);
    }
}
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值