java - Changing Default JSON Time Format with RESTEasy 3.x -


i using resteasy implement rest service using json serialization. currently, dates getting serialized milliseconds since 1970. improve compatibility, dates 1 of 2 formats; milliseconds + timezone offset or iso 8061.

it seems resteasy used use jettison json serialization, i've been reading they've switch jackson ... of has made googling pretty hit or miss.

from can tell, need implement contextresolver along lines of:

    public class jacksonconfig impelments contextresolver<objectmapper>     {         private final objectmapper objectmapper;          public jacksonconfig() throws exception         {             objectmapper = new objectmapper.configure(                                serializationfeature.write_date_as_timestamps, false);         }          @override         public objectmapper getcontext(class<?> arg0)         {             return objectmapper;         }      } 

the thing haven't been able find, do this? put it?

so larger questions are, heading in right direction , assumptions correct?

you need register contextresolver implementation resteasy. can annotating class @provider annotation , allowing resteasy automatically scan during startup, registering in web.xml, or registering in class extends javax.ws.rs.core.application (if how bootstrapping resteasy).

registering via annotations

@provider public class jacksonconfig implements contextresolver<objectmapper> {     private final objectmapper objectmapper;      public jacksonconfig() throws exception     {         objectmapper = new objectmapper.configure(                            serializationfeature.write_date_as_timestamps, false);     }      @override     public objectmapper getcontext(class<?> arg0)     {         return objectmapper;     }  } 

verify classpath scanning enabled in web.xml file so:

<context-param>     <param-name>resteasy.scan</param-name>     <param-value>true</param-value> </context-param> 

note: if deploying in jboss 7 not set resteasy.scan context parameter enabled default.

registering via web.xml

add following context parameter web.xml file. value of parameter should qualified class name of contextresolver.

<context-param>       <param-name>resteasy.providers</param-name>       <param-value>foo.contextresolver.jacksonconfig</paramvalue> </context-param>  

registering via application

if using application class configure resteasy can add provider set of services , providers register resteasy so:

public class myapp extends application  {     @override     public set<class<?>> getclasses()      {         hashset<class<?>> set = new hashset<class<?>>(2);         set.add(jacksonconfig.class);         set.add(myservice.class);         return set;     } } 

more on standalone configuration here


Comments

Popular posts from this blog

iphone - Three second countdown in cocos2d -

hyperlink - how to do url routing in php -

c - Avoiding Extra Malloc in Linked List (node->next = NULL) -