如何在 Spring MVC 中将 HTTP 日期请求标头绑定到 ZonedDateTime?

Rob*_*son 5 java spring datetime spring-mvc java-8

在我的 Spring MVC 控制器中,我想将HTTP 日期格式的标头(例如 If-Modified-Since)绑定到 ZonedDateTime 的实例,如下所示:

@RequestMapping(value = "/foo")
public ResponseEntity<?> someRequest(
    @RequestHeader(value = "If-Modified-Since") ZonedDateTime modifiedSince)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

我已经研究过使用 注释参数@DateTimeFormat,但这并没有提供内置方法来解析 HTTP 日期接受的所有格式。有没有办法让Spring正确绑定标头参数?

小智 0

您必须在接口 @DateTimeFormat 中使用 ISO 选项,如 Spring文档中所示

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
Run Code Online (Sandbox Code Playgroud)