Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

<dependency>     <groupId>com.fasterxml.jackson.datatype</groupId>     <artifactId>jackson-datatype-jsr310</artifactId>     <version>2.7.3</version> </dependency> 

When I try to use a RequestParam with a Java 8 Date/Time type,

@GetMapping("/test") public Page<User> get(     @RequestParam(value = "start", required = false)     @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start) { //... } 

and test it with this URL:

/test?start=2016-10-8T00:00 

I get the following error:

{   "timestamp": 1477528408379,   "status": 400,   "error": "Bad Request",   "exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",   "message": "Failed to convert value of type [java.lang.String] to required type [java.time.LocalDateTime]; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.time.LocalDateTime] for value '2016-10-8T00:00'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2016-10-8T00:00]",   "path": "/test" } 
like image 357
Kery Hu Avatar asked Oct 27 '16 00:10

Kery Hu


People also ask

What is the default format of LocalDateTime?

LocalDateTime is an immutable date-time object that represents a date-time with default format as yyyy-MM-dd-HH-mm-ss.

How do I get strings from LocalDate?

The toString() method of a LocalDate class is used to get this date as a String, such as 2019-01-01. The output will be in the ISO-8601 format uuuu-MM-dd. Parameters: This method does not accepts any parameters. Return value: This method returns String which is the representation of this date, not null.

Is Java time LocalDateTime immutable?

LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second. Other date and time fields, such as day-of-year, day-of-week and week-of-year, can also be accessed. Time is represented to nanosecond precision.


1 Answers

TL;DR - you can capture it as a string with just @RequestParam, or you can have Spring additionally parse the string into a java date / time class via @DateTimeFormat on the parameter as well.

the @RequestParam is enough to grab the date you supply after the = sign, however, it comes into the method as a String. That is why it is throwing the cast exception.

There are a few ways to achieve this:

  1. parse the date yourself, grabbing the value as a string.
@GetMapping("/test") public Page<User> get(@RequestParam(value="start", required = false) String start){      //Create a DateTimeFormatter with your required format:     DateTimeFormatter dateTimeFormat =              new DateTimeFormatter(DateTimeFormatter.BASIC_ISO_DATE);      //Next parse the date from the @RequestParam, specifying the TO type as  a TemporalQuery:    LocalDateTime date = dateTimeFormat.parse(start, LocalDateTime::from);      //Do the rest of your code... } 
  1. Leverage Spring's ability to automatically parse and expect date formats:
@GetMapping("/test") public void processDateTime(@RequestParam("start")                              @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)                              LocalDateTime date) {         // The rest of your code (Spring already parsed the date). } 
like image 168
Bwvolleyball Avatar answered Sep 25 '22 19:09

Bwvolleyball