views:

74

answers:

2

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

Everything is great, but I really don't understand how to set in JSON Marshalling and Unmarshalling converting option 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 createdOn property is serialized, a string like this: '2010-05-12T00:00:00+02:00', but I need to choose date Pattern both, to marshall and unmarshall.

Someone knows hot to do that?

Thank's a lot, Davide.

A: 

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.

redben
Thank's to your reply!Ok for the Javascript extension, however I whanna know to controll the marshall, unmarshall process.Do you know where to extends jersey?Thank's a lot, Davide
Davide
A: 

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;
}
Blaise Doughan
Thank's a lot Blaise! You opened me a world!
Davide
Thank you Blaise, a really good explain
Davide