springboot @PathVariable接收LocalDateTime类型数据

Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDateTime'; 

Text '2021-04-08 05:55:41.934Z' could not be parsed at index 2

LocalDateTime类型的数据给前端,前端再返回来就出现了类型转换的问题

前端穿过来的字段是:2021-04-08T05:55:41.934

下面是我的接口

@GetMapping(value = {"getMsgList/{userId}/{pageSize}/{lastDate}","getMsgList/{userId}/{pageSize}"})
    public Result getMsgList(@PathVariable Integer userId,
                             @PathVariable Integer pageSize,
                             @PathVariable(required = false) LocalDateTime lastDate){
        return websocketService.getMsgList(userId,lastDate,pageSize);
    }

目前能想到的是有两种解决方式,

第一种是,不用LocalDateTime接收数据,直接用String接收数据,接收之后再转成LocalDateTime类型的数据

第二种的增加配置类,虽然第一种也可以,但是我就是不太想用String类型接收时间

下面是配置类

package com.maiya.community.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

import java.time.LocalDateTime;

/**
 * 接口传时间类型参数转换
 */
@Configuration
public class DateConfig {

    @Bean
    public Converter<String, LocalDateTime> localDateTimeConverter(){
        return new Converter<String, LocalDateTime>() {
            @Override
            public LocalDateTime convert(String s) {
                return LocalDateTime.parse(s);
            }
        };
    }
}

加了这个配置类之后,如果想接收localDateTime类型的数据的话,就会先走配置类,把前端传过来的数据转成LocalDateTime类型的数据,

这样就不会出现类型接收异常的问题了

 

方法是可行的,但是要根据前端传过来的具体时间方式来进行处理,处理是活的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

你过来啊~

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值