请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

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

自定义 路由谓词工厂

我的代码

@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



正在回答 回答被采纳积分+3

1回答

大目 2020-10-24 20:25:46

9:24 AM, 6:24 PM

这写法哪儿来的?

我课上不是这样写的吧

0 回复 有任何疑惑可以回复我~
  • 提问者 hthonor #1
    main 函数打印出来的时间格式就是:6:24 PM
    我按照老师的写法也不行。
    
    public static void main(String[] args) {
           DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
           System.out.println(dateTimeFormatter.format(LocalTime.now()));
       }
    回复 有任何疑惑可以回复我~ 2020-10-27 16:20:34
  • 大目 回复 提问者 hthonor #2
    我目前时间格式不是这样的,也没有遇到过这个情况。比较奇怪的是,为嘛end报错,start没有报错呢?
    要不试试把9:24 AM, 6:24 PM改成9:24 AM,6:24 PM,也就是两个时间之间的,去掉。
    =======
    如果还是没能解决的话,帮忙把完整的代码托管到GitHub或者Gitee,我来给你看下。就目前来看,我认为还是你代码层面的问题,不是框架层面的问题。
    回复 有任何疑惑可以回复我~ 2020-10-27 20:15:21
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信