4

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;
    }
Community
  • 1
  • 1
William Kinaan
  • 28,059
  • 20
  • 85
  • 118
  • possible duplicate of [jaxb unmarshal timestamp](http://stackoverflow.com/questions/2519432/jaxb-unmarshal-timestamp) – bdoughan May 24 '13 at 21:15
  • @BlaiseDoughan It is not duplicated, please check the question, It is about `java.sql.date`, not `java.util.date` – William Kinaan May 24 '13 at 21:18

2 Answers2

5

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

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 1
    Thanks for ur answer, I am trying it. – William Kinaan May 24 '13 at 22:00
  • I didn't get the exception, but also I didn't get any results in my browser when trying to call that service. Forthermore, when I tried to call that function locally `public static voic main`, I got the correct data (+1). – William Kinaan May 24 '13 at 22:09
  • I got results in my browser for each other variables in my class, except `EndDate`, It seems like this method isn't triggered itself – William Kinaan May 24 '13 at 22:12
  • I added some information to the question which might be helpful. – William Kinaan May 24 '13 at 22:17
  • 1
    @WilliamKinaan - What if you try a standalone JAXB example outside of JAX-RS to test your mappings. You can set a `ValidationEventHandler` on the `Unmarahaller` to catch any problems. – bdoughan May 24 '13 at 22:17
  • It works buddy, I was calling it `return status` without making new, thanks to your help. – William Kinaan May 24 '13 at 22:22
  • When using java.sql.Date, it gives null pointer exception. Don't forget use java.util.Date because Timestamp extends java.util.Date – Mustafa Kemal May 13 '15 at 06:43
2

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;
Chris
  • 5,584
  • 9
  • 40
  • 58
  • As I have mentioned in the question, I have seen this answer, It is not my case, I get this exception `DateAdapter is not applicable to the field type java.sql.Timestamp. ` – William Kinaan May 24 '13 at 21:12
  • I have already tried your solution, I mention that in the question. It was just that the question didn't show ``, Now I edit it – William Kinaan May 24 '13 at 21:22
  • sorry, I should have made it clear. I think the problem is because you did not mention the types being converted. I have added that in my answer – Chris May 24 '13 at 21:28
  • Thanks to your efforts, +1 – William Kinaan May 24 '13 at 22:23