I am currently working on a RESTful API. I have an Employee class and an EmployeeResource class. I also have a custom DateAdapter, which changes my Date properties to Long timestamps. However, my JSON responses are showing the timestamps as strings (wrapped in double quotes) rather than numbers (without double quotes). Here is an abbreviated version of my code and captured JSON response...
Custom DateAdapter
public class DateAdapter extends XmlAdapter<Long, Date> {
@Override
public Date unmarshal(Long v) throws Exception {
return new Date(Long.valueOf(v));
}
@Override
public Long marshal(Date v) throws Exception {
return v.getTime();
}
}
Entity Class
@Entity
@javax.xml.bind.annotation.XmlRootElement
@XmlType(propOrder={"createdOn","empId"})
public class Employee implements Serializable {
private Date createdOn;
private Integer empId;
@Column(nullable=false)
@Temporal(TemporalType.TIMESTAMP)
@XmlJavaTypeAdapter(DateAdapter.class)
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
@Id
@XmlID
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
}
EmployeeResource
@Path("/Employees")
@javax.xml.bind.annotation.XmlRootElement
@XmlType(propOrder={"hateoas","employees"})
public class EmployeeResource {
List<Employee> employees;
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
@GET
@Path("/{id}")
@Produces("application/json")
public Response getEmployee(@Context UriInfo ui, @PathParam("id") Integer id) {
Session session = HibernateUtil.getSession();
session.beginTransaction();
Criteria criteria=session.createCriteria(Employee.class);
criteria.add(Restrictions.eq("empId", new Integer(10150)));
this.employees = criteria.list();
return Response.ok(this).build();
}
}
Current JSON response
{
"employees":{
"createdOn":"1330915130163",
"empId":"10150"
}
}
Expected JSON response
{
"employees":{
"createdOn":1330915130163,
"empId":10150
}
}
I'm assuming that there's some way to prevent JAXB or JAX-RS from wrapping all numbers in quotes. Could someone guide me to where I could configure this?
Thanks in advance!
EDIT #1 2012.03.07 Ok, so after some more researching, I think my problem is with the default JSONConfiguration.Notation being used, MAPPED . It looks like the NATURAL JSONConfiguration.Notation would get me what I want. However, I haven't found a clear example of how to apply that application wide. I'm assuming I would specify this in my ApplicationConfig class that extends javax.ws.rs.core.Application .
EDIT #2 2012.03.10 Ok, so after some more researching I decided to use the JSON parser library, Jackson. It seems to be the most complete JSON solution out there using just the default configuration. By default Dates are translated to their corresponding timestamps and serialized as numbers (w/o quotes). The only drawback I have come across is that Jackson currently does not support the JAXB annotations, "@XmlID" and "@XmlIDREF". Since I have direct self-references in my data model (not shown above), I have created another question to discuss. If anyone is interested click here to follow that thread...