1

How to send date value to the Calendar object in Webservice Request.

@Produces("application/json")
@Path("Service")
public class ServiceImpl {

    @Context
    HttpServletRequest httpServletRequest;
    @Context
    HttpServletResponse httpServletResponse;

    @POST
    @Path("getDetails")
    @Consumes("application/json")
    @Produces("application/json")
    public String getDetails (ServiceRequest request)
    {
    if(null!=request)
    return "Successfully Parsed given Date";
    }
}

public class ServiceRequest {

    private XMLGregorianCalendar requestDate;

    void setDate(XMLGregorianCalendar date){
    this.requestDate = date;
    }
    XMLGregorianCalendar getDate()
    {
    return requestDate;
    }
}

Request URL: http://localhost:7001/WebProject/Service/getDetails

RequestParams:

{
"requestDate" : "2015-04-20T05:30:00.000Z"
}

And it is not parsing the value as date. How could i pass value for Calendar object.

Am using genson jar to serialise the request. And the error message is like

Caused by: com.owlike.genson.JsonBindingException: Could not parse date 2015-04-20T05:11:00.000Z
    at com.owlike.genson.convert.DefaultConverters$DateConverter.deserialize(DefaultConverters.java:1003)
    at com.owlike.genson.convert.DefaultConverters$CalendarConverter.deserialize(DefaultConverters.java:1226)
    at com.owlike.genson.convert.DefaultConverters$CalendarConverter.deserialize(DefaultConverters.java:1209)
    at com.owlike.genson.convert.BeanViewConverter.deserialize(BeanViewConverter.java:102)
    at com.owlike.genson.convert.NullConverter$NullConverterWrapper.deserialize(NullConverter.java:56)
    at com.owlike.genson.reflect.PropertyMutator.deserialize(PropertyMutator.java:30)
    ... 48 more
Caused by: java.text.ParseException: Unparseable date: "2015-04-20T05:11:00.000Z"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at com.owlike.genson.convert.DefaultConverters$DateConverter.read(DefaultConverters.java:1009)
    at com.owlike.genson.convert.DefaultConverters$DateConverter.deserialize(DefaultConverters.java:1001)
    ... 53 more
Arun
  • 609
  • 2
  • 12
  • 33
  • 1
    These two threads may help: http://stackoverflow.com/a/26452897/4797507 http://stackoverflow.com/questions/13616034/how-do-i-configure-the-date-formatter-through-genson-jersey – Bob Aleena Apr 21 '15 at 05:07
  • 1
    Thanks @Bob. It worked for me when i write provider class as it is mentioned in the docs. – Arun Apr 21 '15 at 05:57
  • Adding to this i have some problem when getting response from the service. Say my data base is located in different location and it has different location. When i debug my code i could find the date and time is perfectly matched, but when genson provider comes in place it converts to simple date format of my current system located time. Which mean it is converting to system time and not with reference value – Arun Jun 08 '15 at 06:06

1 Answers1

1

If you are using Genson 1.2 and older than yes the two answers pointed in the comments should work for you. Starting with Genson 1.3 there has been a slight refactoring around configuring the JAX-RS extension, so now it should be easier to do. An example using Jersey:

Genson genson = new GensonBuilder()
  .useDateFormat(yourDateFormat)
  .create();

new ResourceConfig().register(new GensonJaxRSFeature().use(genson));

But the old way to register a custom instance should still continue to work with 1.3 and up.

Here are the relevant docs about jax-rs and configuration.

eugen
  • 5,856
  • 2
  • 29
  • 26
  • Yes eugen, It worked very well with date in serializing and de-serializing. But when this provider class is invoked, it is converting to system data, though you have different value in object – Arun Jun 08 '15 at 10:42
  • Oh it is the XmlGregorianCalendar, this ones date format is not yet configurable, but you can have a look at [the source code](https://github.com/owlike/genson/blob/master/genson/src/main/java/com/owlike/genson/ext/jaxb/JAXBBundle.java#L113) to see how it is implemented and try out with your own Converter or use a basic date/calendar for which the custom date formats are actually used. – eugen Jun 08 '15 at 12:41
  • @Weedoze you can either register it with ResourceConfig or Client. – abbas Oct 31 '17 at 04:13