How to use LocalDateTime RequestParam in Spring? I get “Failed to convert String to LocalDateTime”

后端 未结 10 1439
小蘑菇
小蘑菇 2020-11-29 02:35

I use Spring Boot and included jackson-datatype-jsr310 with Maven:


    com.fasterxml.jackson.datatype         


        
10条回答
  •  孤城傲影
    2020-11-29 02:35

    You did everything correct :) . Here is an example that shows exactly what you are doing. Just Annotate your RequestParam with @DateTimeFormat. There is no need for special GenericConversionService or manual conversion in the controller. This blog post writes about it.

    @RestController
    @RequestMapping("/api/datetime/")
    final class DateTimeController {
    
        @RequestMapping(value = "datetime", method = RequestMethod.POST)
        public void processDateTime(@RequestParam("datetime") 
                                    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateAndTime) {
            //Do stuff
        }
    }
    

    I guess you had an issue with the format. On my setup everything works well.

提交回复
热议问题