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.
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.
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);
}
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?
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 - http://www.baeldung.com/spring-mvc-custom-data-binder
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);
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加