Jersey how to annotate java.sql.Timestamp object

Background

I have a Root element class that contains a variable of the java.sql.Timestamp class, I want JAXB to create an xml element from this variable.

What i tried

  • I am creating a class adapter that is.

import java.sql.Date;

import java.sql.Timestamp;

import javax.xml.bind.annotation.adapters.XmlAdapter;

 public class TimestampAdapter extends XmlAdapter <Date, Timestamp> { 
  public Date marshal(Timestamp v) { return new Date(v.getTime()); } public Timestamp unmarshal(Date v) { return new Timestamp(v.getTime()); } 

}

  • . Then I annotate the function that receives this variable:

@XmlJavaTypeAdapter (TimestampAdapter.class)
public java.sql.Timestamp getEndDate () {

if (endDate == null)

retrieveInfo ();

return endDate;

}

Problem

I still get this exception

 java.sql.Date does not have a no-arg default constructor. 

I also checked this thread , but it talks about String for TimeStamp, but not in my case.

Any help would be appreciated.

EDIT

This variable is in the OrderStatus class, I call it the OrderImpl class as follows

 @Override @XmlElement(name = "Status", type = OrderStatus.class) public OrderStatus getStatus() { return status; } 
+4
source share
2 answers

Your XmlAdapter should convert Timestamp to / from java.util.Date instead of java.sql.Date .

+5
source

Your adapter should be like this:

 public class TimestampAdapter extends XmlAdapter<Date, Timestamp> { public Date marshal(Timestamp v) { return new Date(v.getTime()); } public Timestamp unmarshal(Date v) { return new Timestamp(v.getTime()); } } 

and

 @XmlJavaTypeAdapter( TimestampAdapter.class) public Timestamp done_date; 
+2
source

Source: https://habr.com/ru/post/1482647/


All Articles