Failed to bind properties under 'end' to java.time.LocalTime:

来源:10-7 自定义路由谓词工厂

hthonor

2020-10-23

自定义 路由谓词工厂

我的代码

@Component
public class TimeBetweenRoutePredicateFactory
extends AbstractRoutePredicateFactory<TimeBetweenConfig> {
   public TimeBetweenRoutePredicateFactory() {
       super(TimeBetweenConfig.class);
   }

   @Override
   public Predicate<ServerWebExchange> apply(TimeBetweenConfig config) {
       LocalTime start = config.getStart();
       LocalTime end = config.getEnd();

       return exchange -> {
           LocalTime now = LocalTime.now();
           return now.isAfter(start) && now.isBefore(end);
       };
   }

   /**
    * 配置类 与 配置文件 映射关系
    * @return
    */
   @Override
   public List<String> shortcutFieldOrder() {
       return Arrays.asList("start","end");
   }

   public static void main(String[] args) {
       DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
       System.out.println(dateTimeFormatter.format(LocalTime.now()));
   }
}


main函数 打印的时间格式是:“6:36 PM”

所以我的配置如下:

gateway:
 discovery:
   locator:
     #让 gateway 通过服务发现组件,找到其他微服务
     enabled: true
 routes:
   - id: time_between
     uri: lb://user-center
     predicates:
       - TimeBetween=9:24 AM, 6:24 PM


应用启动报错:

2020-10-23 18:40:15.257 ERROR 9429 --- [ask-Scheduler-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task

reactor.core.Exceptions$ErrorCallbackNotImplemented: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'end' to java.time.LocalTime

Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'end' to java.time.LocalTime

Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalTime] for value '6:24 PM'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [6:24 PM]

Caused by: java.lang.IllegalArgumentException: Parse attempt failed for value [6:24 PM]

Caused by: java.time.format.DateTimeParseException: Text '6:24 PM' could not be parsed at index 0


Failed to bind properties under 'end' to java.time.LocalTime:

    Property: end

    Value: 6:24 PM

    Origin: "end" from property source "source"

    Reason: failed to convert java.lang.String to java.time.LocalTime

Action:

Update your application's configuration



写回答

1回答

大目

2020-10-24

9:24 AM, 6:24 PM

这写法哪儿来的?

我课上不是这样写的吧

0
2
大目
回复
hthonor
我目前时间格式不是这样的,也没有遇到过这个情况。比较奇怪的是,为嘛end报错,start没有报错呢? 要不试试把9:24 AM, 6:24 PM改成9:24 AM,6:24 PM,也就是两个时间之间的,去掉。 ======= 如果还是没能解决的话,帮忙把完整的代码托管到GitHub或者Gitee,我来给你看下。就目前来看,我认为还是你代码层面的问题,不是框架层面的问题。
2020-10-27
共2条回复

Spring Cloud Alibaba微服务从入门到进阶

面向未来微服务:熟练掌握Spring Cloud Alibaba

3103 学习 · 1326 问题

查看课程