java - Spring @RequestParam DateTime format as ISO 8601 Date Optional Time -
i'm using spring framework services api , org.joda.time.datetime
datetime parsing. specifically, i'm using isoformatter.dateoptionaltimeparser()
, allows users flexibility use date, or both date , time, requirement.
believe me, i've seen these related questions can tell people going point me towards, e.g. this , this, etc.
previously, taking date string
, processing using joda formatter mentioned above in service layer, want add request validation in controller, means if request syntactically incorrect, request shouldn't go service layer.
i've tried using multiple variations of @datetimeformat(iso = iso.date_time)
, specifying pattern
string in format thing 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 ensure date user complies iso 8601 dateoptionaltime
format? can maybe apply multiple patterns implement this?
you can create converter , take care of it. have used offsetdatetime in example below, can replaced localdatetime. detailed article, refer url - http://www.baeldung.com/spring-mvc-custom-data-binder
even struggling sometime , wasn't working. trick use @component
annotation , did 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); } }
Comments
Post a Comment