json - How can I have JAX-RS return a Java 8 LocalDateTime property as a JavaScript-style Date String? -
json - How can I have JAX-RS return a Java 8 LocalDateTime property as a JavaScript-style Date String? -
i created restful web service using jax-rs method annotations:
@get @path("/test") @produces(mediatype.application_json) public mything test() { mything myobject = new mything(localdatetime.now()); homecoming myobject; }
this works nicely, i'd adjust 1 thing: if returned java object contains property of new java 8 localdatetime type, represented json object:
{"mydatetimeproperty":{"hour":14,"minute":32,"second":39,"year":2014,"month":"november","dayofmonth":6,"dayofweek":"thursday","dayofyear":310,"monthvalue":11,"nano":0,"chronology":{"calendartype":"iso8601","id":"iso"}},...}
how can tell jax-rs homecoming javascript date.tojson()-style string like
{"mydatetimeproperty":"2014-11-07t15:06:36.545z",...}
instead?
i've never utilize localdatetime
before, decided testing. here findings:
jersy 2.13 , provider (works out box no configuration)
<dependency> <groupid>org.glassfish.jersey.media</groupid> <artifactid>jersey-media-moxy</artifactid> <version>${jersey.version}</version> </dependency>
jersey 2.13 provider (has back upwards jaxb annotation - dependency on jackson-module-jaxb-annotations), custom adapter
<dependency> <groupid>org.glassfish.jersey.media</groupid> <artifactid>jersey-media-json-jackson</artifactid> <version>${jersey.version}</version> </dependency> public class localdatetimeadapter extends xmladapter<string, localdatetime> { @override public localdatetime unmarshal(string s) throws exception { homecoming localdatetime.parse(s); } @override public string marshal(localdatetime datetime) throws exception { homecoming datetime.tostring(); } } // getter model class @xmljavatypeadapter(localdatetimeadapter.class) public localdatetime getdatetime() { homecoming datetime; }
resteasy 3.0.9 provider, (also has back upwards jaxb annotation - dependency on jackson-module-jaxb-annotations), custom adapter (see above)
<dependency> <groupid>org.jboss.resteasy</groupid> <artifactid>resteasy-jackson2-provider</artifactid> <version>${resteasy.version}</version> </dependency>
both resteasy , bailiwick of jersey dependency (also did not work without custom config, same lastly 2 - adapter)
<dependency> <groupid>com.fasterxml.jackson.jaxrs</groupid> <artifactid>jackson-jaxrs-json-provider</artifactid> <version>2.4.0</version> </dependency>
we need create sure register jacksonjaxbjsonprovider
so guess seems provider uses jackson, not give deisred result, without custom configuration, whether through adapter (as seen above) or other custom configuration. jersey-media-moxy
provider doesn't utilize jackson.
java json jax-rs java-8 java-time
Comments
Post a Comment