Skip to content

The problem with RequestParam/DateTimeFormat and LocalDateTime #25878

Closed
@AndrewBystrov

Description

@AndrewBystrov

Affects: \5.1.6
I have the following code

@GetMapping("/somepath")
public String getNextDayOfMonth(
    @RequestParam(value = "target_date", required = true)
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    LocalDate targetDate
) {
    return targetDate.plusDays(1).getDayOfMonth();
}

But when I execute this handler with path like /sompath?target_date=, I get NullPointerException, because my variable target_date is null, but I define required=true and DateTimeFormat annotation. I thought, if I set DateTimeFormat property and execute this mapping with invalid date string (empty in my case), I will get client exception (4xx codes) instead of server exception (5xx, cause NPE it is server side exception).

I'm suspect, that problem in this line of code:

String text = (String) source;
if (!StringUtils.hasText(text)) {
return null;
}

and think, that passing empty string for date - it's client error


Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)status: duplicateA duplicate of another issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions