7

I'm using Jersey (jax-rs), to build a REST rich application.

Everything is great, but I don't really understand how to configure the JSON Marshalling/Unmarshalling options for dates and numbers.

I have a User class:

@XmlRootElement
public class User {
    private String username;
    private String password;
    private java.util.Date createdOn;

    // ... getters and setters
}

When the createdOn property is serialized, I get a string like this: '2010-05-12T00:00:00+02:00', but I need to use a specific date pattern, both to marshall and unmarshall dates.

Does someone know how to do that?

RustyTheBoyRobot
  • 5,891
  • 4
  • 36
  • 55
Davide
  • 602
  • 7
  • 18

3 Answers3

16

You could write an XmlAdapter:

Your particular XmlAdapter would look something like:

import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class JsonDateAdapter extends XmlAdapter<String, Date> {

    @Override
    public Date unmarshal(String v) throws Exception {
        // TODO convert from your format
    }

    @Override
    public String marshal(Date v) throws Exception {
        // TODO convert to your format
    }

}

Then on your date property set the following annotation:

@XmlJavaTypeAdapter(JsonDateAdapter.class)
public getDate() {
   return date;
}
bdoughan
  • 147,609
  • 23
  • 300
  • 400
2

What you get is a date ISO 8601 format, which is a standard. Jersey will parse it for you on the server. For javascript here is an extension to js date to parse that.

Update Link is dead: try another parser, see Help parsing ISO 8601 date in Javascript

redben
  • 5,578
  • 5
  • 47
  • 63
1

If you do not want to have to play with the adaptors or you needed custom marshalling for different objects and want to avoid the adaptors alltogether, you could also play with the attributes and the bean pattern:

private Date startDate;

@XmlTransient
public Date getStartDate() {
    return startDate;
}
public void setStartDate(Date startDate) {
    this.startDate = startDate;
}
@XmlElement public String getStrStartDate() {
    if (startDate == null) return null;
    return "the string"; // the date converted to the format of your choice with a DateFormatter";
}
public void setStrStartDate(String strStartDate) throws Exception {
    this.startDate = theDate; // the strStartDate converted to the a Date from the format of your choice with a DateFormatter;
}
  • After a few of development days, I'm using both, a powerful js library (http://www.datejs.com/), to convert data from and to server, in ISO-8601 format and, when needed, I use your work around. The need come's up, when there is a different Time Zone from between client to server. In some ways, Date is automatically calculated, using the difference between Local Time and GMT. A big trouble when you need a date, trunked to midnight... – Davide Feb 15 '11 at 23:44
  • 1
    @Davide I found the [joda-time](http://joda-time.sourceforge.net/) library useful to parse dates in various string formats and to calculate the last midnight at/before a given time stamp. – Stefan L Feb 02 '12 at 10:08