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

 
Java.sql.Date does not have a no-arg default constructor
Java.sql.Time does not have a no-arg default constructor
Java.sql.Timestamp does not have a no-arg default constructor
 
These are exceptions raised when converting an object to XML especially when dealing with jax-ws or at the time of creating WSDL in bottom up approach.
 

 
 
In XML based web service we use XML as data transporter i.e object passed to a web service and object returned by the web service  will be in XML format.
 
Java Architecture for XML Binding (JAXB) will not have any problem in convection (object to XML) if the subjected objects has a default constructor. In case if you have an object without default constructor then it should not be a surprising issue to see an exception saying  “does not have a no-arg default constructor”.
 
 
How to reslove
Solution to this problem is to add an simple XmlAdapter for the objects.
 
Say I have Bean class like this

import java.sql.Date;
public class User {

private Date dob ;

public Date getDob() {
return dob ;
}
public void setDob(Date dob) {
this.dob = dob;
}

 
As you might have already guessed that Date object in User class will create problem as Date doesn’t have default constructor. So adding an XmlAdapter will defend from getting caught by exceptions.
 
 
Creating simple a  XmlAdapter
 

import java.sql.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class SqlDateAdapter extends XmlAdapter <String , Date>
{

public String marshal(java.sql.Date d) {
return d.toString();
}

public Date unmarshal(String v) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(QueryConstants.DATE_FORMATE);
    java.sql.Date sqlDate = null;
   try {
             java.util.Date convertedDate = dateFormat.parse(v);
             sqlDate = new java.sql.Date(convertedDate.getTime());
} catch (ParseException e) {
       e.printStackTrace();
}
return sqlDate;
}
}

Then put @XmlJavaTypeAdapter on a place where your code references for Date, like this.
 

public class User {
private Date dob ;

@XmlJavaTypeAdapter(Date.class)
public Date getDob() {
return dob ;
}
public void setDob(Date dob) {
this .dob = dob;
}
}

Thats it, you have told JAXB that how to handle an your objects that doest no have default constructor.
 
 
 
It would be tiresome job  to annotate each and every piece  of code if you have more than one  problem creating object like this
 

private Date myDob ;
private Date fatherDob ;
private Date motherDob ;
private Date sisterDob ;
...
...
private Date uncleDob ;

alternative solution to added  @XmlJavaTypeAdapter annotation is to define the annotation in package level
 
Adding package level annotation
 
Create a file package-info.java in directory where your problem creating objects are located. And then add the following code snippet.
 

@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter (value=com.shakthydoss.SqlDateAdapter .class,type=java.sql.Date.class)
package com.shakthydoss.beans;

Thats it, you have told JAXB that that how to handle an your objects in the package ( com.shakthydoss.beans) that don’t have default argument.

3 Comments

  1. Rua Thad wrote
    at 3:42 PM - 22nd October 2012 Permalink

    Thank you very much. I wasted so much time to slove this problem.

  2. Juse Davi wrote
    at 9:05 AM - 29th October 2012 Permalink

    Thank you for posting

  3. Juse davi wrote
    at 8:55 PM - 29th December 2012 Permalink

    this really saved my time

Post a Comment

Your email is never published nor shared. Required fields are marked *