Alexander
Alexander

Reputation: 85

why cxf jax-rs date field serialization have colon?

my class-entity for rest cxf service has a Date field. The format from the Jettison JsonProvider is:

"date":"2012-08-13T16:40:07.281-03:00"

But from the GSon client it's formatted for iso8601 compliance (without colon timezone) as:

"date":"2012-08-13T16:40:07.281-0300" .

What happened with jettison jax-rs?

Is there any simple way in CXF Jettison to specify the date-timezone format?

Thanks

Upvotes: 2

Views: 948

Answers (2)

Alexander
Alexander

Reputation: 85

@user1036 -thanks for you advice.This is my old issue.In those time I've resolved it by use cxf handler/interceptor and insert colon between time part before send operation.thanks.

Upvotes: 0

k_o_
k_o_

Reputation: 6298

You can use a org.apache.cxf.jaxrs.ext.ParameterHandler:

@Component
public class DateHandler implements ParameterHandler<Date> {

      public Date fromString(String s) {
          DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
          return fmt.parseDateTime(s).toDateTime(DateTimeZone.UTC).toDate();
      }
}  

You must get hold of the ServerFactoryBean while it is constructed. This can be done in Spring in the XML configuration and it could be implemented with Spring Java config. I have used Spring Java config.

@Bean
public List<Object> jaxRSProviders() {
    return new ArrayList<>(Arrays.asList(Your other providers, e.g. error handlers, fault barriers
           dateHandler));
}

JAXRSServerFactoryBean factory = ...
providers.addAll(jaxRSProviders);
factory.create();

Upvotes: 2

Related Questions