java - @JsonFormat for date parsing in JAX-RS service is ignored -
i'm trying pass date jax-rs service. checking other questions like: date format mapping json jackson
the answers , documentation show there jackson annotation should allow date formatting.
public class solutionfilter { @matrixparam("todate") @jsonformat(shape=jsonformat.shape.string, pattern="yyyy-mm-dd", timezone="cet") private date todate; public void settodate(date todate) { this.todate = todate; } }
upon calling rest-service i'm getting parseexception
:
caused by: java.text.parseexception: unparseable date: "2016-01-01" @ java.text.dateformat.parse(dateformat.java:366) @ org.glassfish.jersey.message.internal.httpdateformat.readdate(httpdateformat.java:137) @ org.glassfish.jersey.server.internal.inject.paramconverters$dateprovider$1.fromstring(paramconverters.java:259)
it seems annotation ignored. debugging parse method pattern set eee, dd mmm yyyy hh:mm:ss zzz
, eee mmm d hh:mm:ss yyyy
.
i'm using spring 4.2.1, jersey 2.22 binds jackson 2.5.4.
how can dates parsed correct pattern?
update: thinking further json used output parsing. jax-rs parameter parsing.
param conversion done paramconverter
s. if follow stacktrace, see paramconverters$dateprovider. next call httpdateformat
class parsing.
if @ top of class, see date formats supported. these standard http data formats
/** * date format pattern rfc 1123. */ private static final string rfc1123_date_format_pattern = "eee, dd mmm yyyy hh:mm:ss zzz"; /** * date format pattern rfc 1036. */ private static final string rfc1036_date_format_pattern = "eeee, dd-mmm-yy hh:mm:ss zzz"; /** * date format pattern ansi c asctime(). */ private static final string ansi_c_asctime_date_format_pattern = "eee mmm d hh:mm:ss yyyy";
as far know or can tell, there no configuration available can add list. other option write own converter. example
@provider public class dateparamconverterprovider implements paramconverterprovider { private final string format; public dateparamconverterprovider(string dateformat) { this.format = dateformat; } @override public <t> paramconverter<t> getconverter(class<t> rawtype, type generictype, annotation[] annotations) { if (rawtype != date.class) { return null; } return (paramconverter<t>) new paramconverter<date>() { @override public date fromstring(string value) { simpledateformat formatter = new simpledateformat(format); try { return formatter.parse(value); } catch (exception ex) { throw new webapplicationexception("bad formatted date", 400); } } @override public string tostring(date date) { return new simpledateformat(format).format(date); } }; } }
here complete test case using jersey test framework
public class dateparamtest extends jerseytest { private static final string format = "mm-dd-yyyy"; @path("date") public static class dateresource { @get public string get(@matrixparam("since") date date) { return new simpledateformat(format).format(date); } } @override public resourceconfig configure() { return new resourceconfig(dateresource.class) .register(new dateparamconverterprovider(format)); } @test public void should_return_same_date_and_format() { final string date = "09-30-2015"; response response = target("date").matrixparam("since", date) .request().get(); assertequals(200, response.getstatus()); string returndate = response.readentity(string.class); assertequals(date, returndate); system.out.println(returndate); } }
here dependency test framework
<dependency> <groupid>org.glassfish.jersey.test-framework.providers</groupid> <artifactid>jersey-test-framework-provider-grizzly2</artifactid> <version>${jersey2.version}</version> <scope>test</scope> </dependency>
see also:
- read parameter within jersey's paramconverter example.
- how in-memory unit test spring-jersey example of using test framework spring-jersey app.
Comments
Post a Comment