您当前的位置: 首页 >  ar

梁云亮

暂无认证

  • 2浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringMVC 自定义日期时间转换器时报错:Unable to determine source type <S> and target type <T> for your Converter

梁云亮 发布时间:2022-07-08 01:19:19 ,浏览量:2

错误

在这里插入图片描述

解决方案
@Configuration
public class DateConverterConfig {

    @Bean
    @ConditionalOnBean(name = "requestMappingHandlerAdapter") //不加该行代码报错:Unable to determine source type  and target type  for your Converter [com.wego.config.DateConverterConfig$$Lambda$828/0x00000007c08f57b0]; does the class parameterize those types?
    public Converter localDateTimeConverter() {
        return source -> LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    @Bean
    public Converter localDateConverter() {
        return new Converter() {
            @Override
            public LocalDate convert(String source) {
                return LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            }
        };
    }
}

说明:采用传统方式书写时不会报错,但优化代码为lambda时就报错了,解决方案,只需要在方法上面添加注解@ConditionalOnBean(name = "requestMappingHandlerAdapter")就可以了

关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.1213s