Monday, 10 December 2012

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

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

Problem : Usually JAXB expects a default constructor for the objects which it needs marshalling (object to XML).
You face problems when you use java.sql.Timestamp, java.sql.Date etc., object which does not have a no-arg constructor.

Solution : create the following adapter classes and info classes and build again. This should work.

1. Create a Timestamp adapter class
package project1.adapters;
import java.sql.Timestamp;
import java.util.Date;
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());
      }
  }


2. create a package-info.java at your project1 level.
@XmlJavaTypeAdapters( 
 @XmlJavaTypeAdapter(value=TimestampAdapter.class,type=Timestamp.class))
package project1;

import java.sql.Timestamp;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;

import project1.adapters.TimestampAdapter;
 

1 comment:

  1. Can you please expand on how to do the step 2. Eclipse does not allow creating package-info.java.

    ReplyDelete