Affects: Spring-Framework:5.3.1, spring-boot:2.4.0


Expected The endpoints in controllers should parse the String value to the Date value.

Actual The endpoints in controllers throw the TypeMismatchException exception.

("org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-11-26'; nested exception is java.lang.IllegalArgumentException") and respond with 400 to the client.
@RestController
public class MyController {

    @GetMapping
    public String get(Date date) {
        return date.toString();
    }

}

Spring Cannot convert the string value to the Date value in controllers of WebFlux

Comment From: snicoll

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

Your @GetMapping does not have any path variable declaration to bind the parameter. Please review the documentation

Comment From: JamesChenX

Hi, @snicoll, this is a bug and there is nothing to do with the path variable. For the following cases: 1. http://localhost:9912/test1?date=2020-11-26. 400. 2. http://localhost:9912/test2?date=2020-11-26. 200, and works fine.

@RestController
public class MyController {

    @GetMapping("test1")
    public String get(Date date) {
        return date.toString();
    }

    @GetMapping("test2")
    public String get(String date) {
        return date;
    }

}

Comment From: snicoll

Ah, right. I got tricked by the fact that @RequestParam was missing. The same happens with SpringMvc by the way which is no surprise really. It looks like we don't have a converter for java.util.Date there.

Comment From: rstoyanchev

@JamesChenX please text code snippets instead of pasting images for simple code. They are more friendly for search and for copy+paste when needed. You might want to check out this Mastering Markdown guide for future reference.

Comment From: JamesChenX

@rstoyanchev OK, I have replaced the image with the code snippet. Thanks for reminding.

Comment From: encircled

Hi, just register a custom type converter from string to date with required date format. https://stackoverflow.com/questions/35025550/register-spring-converter-programmatically-in-spring-boot/41205653

Comment From: JamesChenX

@encircled Thanks. I know how to make it works but Spring should provide such a type converter because it's a very common requirement for any Web application.

Comment From: rstoyanchev

@JamesChenX you can use java.time.* or otherwise if you really want java.util.Date then configure a DateFormatter via WebMvcConfigurer#addFormatters.

Comment From: spring-projects-issues

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

Comment From: spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.