spring boot2 RequestParam支持时间参数

通过WebMvcConfigurer 的addFormatters接口

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        DateTimeFormatterRegistrar dateTimeFormatterRegistrar = new DateTimeFormatterRegistrar();
        dateTimeFormatterRegistrar.setTimeFormatter(DateTimeFormatter.ISO_TIME);
        dateTimeFormatterRegistrar.setDateFormatter(DateTimeFormatter.ISO_DATE);
        dateTimeFormatterRegistrar.setDateTimeFormatter(ofPattern("yyyy-MM-dd HH:mm:ss"));
        dateTimeFormatterRegistrar.registerFormatters(registry);
    }
}

controller中使用

@RestController
@RequestMapping("/demo")
public class DemoController {

    @GetMapping("/test-get")
    public String testGetRequest(@RequestParam("time") LocalDate time){
        return time.toString();
    }

测试一下

http get
http://localhost/demo/test-get?time=2019-12-01

你可能感兴趣的:(spring boot2 RequestParam支持时间参数)