Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey how to annotate java.sql.Timestamp object

Background

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

What I have tried

  • I create class adapter which 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 which gets that 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.

Also I have checked This Thread, but it is talking about String to TimeStamp, not my case.

Any help would be appreciated.

EDIT

This variable is in the class OrderStatus, I call it from class OrderImpl like this

@Override
    @XmlElement(name = "Status", type = OrderStatus.class)
    public OrderStatus getStatus() {
        return status;
    }
like image 403
William Kinaan Avatar asked Mar 24 '23 20:03

William Kinaan


2 Answers

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

like image 192
bdoughan Avatar answered Apr 02 '23 04:04

bdoughan


Your adapter needs to 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;
like image 31
Chris Avatar answered Apr 02 '23 05:04

Chris