spring-boot-web和java8 datetime

使用LocalDateTime作为参数的时候会解析失败:

@GetMapping("/datetime")
public String datetime(@RequestParam LocalDateTime datetime) {

需要加入如下配置:

@Configuration
public class LocalDateTimeConfig {
    @Bean
    public Formatter localDateFormatter() {
        return new Formatter() {
            @Override
            public LocalDateTime parse(String s, Locale locale) throws ParseException {
                return LocalDateTime.parse(s, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
            }

            @Override
            public String print(LocalDateTime localDateTime, Locale locale) {
                return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(localDateTime);
            }
        };
    }
}

然后使用FeignClient调用的时候, 在FeignClient这端也会产生一个解析问题, 没有正确的解析ISO_8601格式

解决方法: 新增一个配置:

@Bean
public Contract feignContract() {
    return new SpringMvcContract();
}

就可以使用FeignClient传递LocalDateTime对象了

例子代码见: https://gitee.com/liuxy9999/mytest-spring-cloud

你可能感兴趣的:(spring-boot-web和java8 datetime)