Spring @RequestParam DateTime格式为ISO 8601日期可选时间

编程入门 行业动态 更新时间:2024-10-27 14:35:00
本文介绍了Spring @RequestParam DateTime格式为ISO 8601日期可选时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我将Spring Framework用于我的服务API,并将org.joda.time.DateTime用于日期时间解析.具体来说,我使用的是ISOFormatter.dateOptionalTimeParser(),它使用户可以灵活地仅使用日期,或者同时使用日期和时间,这是必须的.

I'm using Spring Framework for my services API and org.joda.time.DateTime for datetime parsing. Specifically, I'm using the ISOFormatter.dateOptionalTimeParser(), which allows users the flexibility to use just the date, or both date and time, which is a requirement.

相信我,我已经看到了所有这些我可以告诉人们的相关问题,例如, 此和此等

Believe me, I've seen all these related questions that I can already tell people are going to point me towards, e.g. this and this, etc.

以前,我将日期作为String,然后在服务层中使用上面提到的joda格式化程序对其进行处理,但是现在我想在控制器中添加请求验证,这意味着如果请求在语法上不正确,该请求甚至都不应转到服务层.

Previously, I was taking the date as String and then processing it using the joda formatter mentioned above in the service layer, but now I want to add request validation in the controller, which means that if the request is syntactically incorrect, the request shouldn't even go to the service layer.

我尝试使用@DateTimeFormat(iso = ISO.DATE_TIME)的多个变体,以及以格式形式指定pattern字符串,但没有任何运气.

I've tried using multiple variations of @DateTimeFormat(iso = ISO.DATE_TIME), as well as specifying the pattern String in format thing with no luck, whatsoever.

@RequestMapping(value = URIConstants.TEST_URL, method = RequestMethod.GET) public @ResponseBody String getData(@RequestParam(required = false) DateTime from, @RequestParam(required = false) DateTime to) { return dataService.fetchDataFromDB(from, to); }

我该怎么做才能确保我从用户那里得到的日期符合ISO 8601 dateOptionalTime格式?我可以运用多种模式来实现吗?

What should I do to ensure that the date I get from user complies with the ISO 8601 dateOptionalTime format? Can I maybe apply multiple patterns to implement this?

推荐答案

您还可以创建一个转换器,它将进行处理.我在下面的示例中使用了OffsetDateTime,但是可以轻松地将其替换为LocalDateTime.有关详细的文章,请参考以下网址- www.baeldung/spring-mvc-custom-data-binder

You can also create a converter and that will take care of it. I have used OffsetDateTime in the example below, but that can be easily replaced with LocalDateTime. For a detailed article, refer this url - www.baeldung/spring-mvc-custom-data-binder

即使我为此苦了一段时间,也没有用.诀窍是使用@Component批注为我完成.

Even I was struggling with this for sometime and it wasn't working. The trick is to use the @Component annotation and did it for me.

import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; @Component public class OffsetDateTimeConverter implements Converter<String, OffsetDateTime> { @Override public OffsetDateTime convert(final String source) { if (source == null || source.isEmpty()) { return null; } return OffsetDateTime.parse(source, DateTimeFormatter.ISO_OFFSET_DATE_TIME); } }

更多推荐

Spring @RequestParam DateTime格式为ISO 8601日期可选时间

本文发布于:2023-11-03 16:53:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可选   格式为   日期   时间   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!