Link to home
Start Free TrialLog in
Avatar of HycelTaylor
HycelTaylor

asked on

Defining Timestamp for use in the WSDL

Hello,

I've created a simple java value object to test the different primary attributes that I will be using throughout my application which will be using web services.  This value object will also be used in an interface to pass data to the client in a web service.  
 
The value object is as follows:
 
public class InformationVO {
  private String name;
  private Integer Id;
  private Float price;
  private Timestamp creationDate;
  private Boolean inStock;
 
  ....
}
 
The interface is as follows:
 
pubic interface Information {
  public InformationVO[] getInformationList();
  public InformationVO getInformation(Integer id);
}
 
Problem:
 
When I run the java org.apache.axis.wsdl.Java2WSDL program for the Information interface I get the following error:

"The class java.sql.Timestamp is defined in java or javax package and cannot be converted into an xml schema type.  an xml schema anyType will be used to define this class in wsdl file."
 
I looked on line at http://ws.apache.org/axis/java/user-guide.html, under the topic of "Standard mappings from WSDL to Java" and found that xsd:dateTime maps to java.util.Calendar.  
 
I'm not sure how this will work since java.util.Calendar is an abstract class.  I wouldn't want to use GregorianCalendar all over the place when Timestamp will do just fine.  I use Timestamp all over the place.  Thus, there must be a way to map Timestamp to xsd:dateTime or create a custom serializer and de-serializer in order to use Timestamp.
 
Has any one run in to this issue?  

If so, what was your solution?
 
Thanks,

Hycel
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Simplest would be to use xsd:long:

private long creationDate;


...

creationDate = System.currentTimeMillis();
Avatar of HycelTaylor
HycelTaylor

ASKER

CEHJ

That's actually not a bad solution.  Still it would mean that I would need to modify all of my value objects that use Timestamp as well as the data access objects that use those value object and all of the business logic that surounds them.  

Do you not think that perhaps someone else has run in to this same problem and found a solution where Timestamp can still be used?  I am a little confused as to why the experts that defined the java specification for web services chose to use the java.util.Calendar class as the standard mapping for WSDL.  Below is a list of there mappings:

Standard mappings from WSDL to Java

xsd:base64Binary byte[]
xsd:boolean boolean
xsd:byte byte
xsd:dateTime java.util.Calendar
xsd:decimal java.math.BigDecimal
xsd:double double
xsd:float float
xsd:hexBinary byte[]
xsd:int int
xsd:integer java.math.BigInteger
xsd:long long
xsd:QName javax.xml.namespace.QName
xsd:short short
xsd:string java.lang.String

I just don't get it.  The java.sql.Timestamp is used by so many java programmers in there VO's.  I'm still of the belief that there is a mapping solution.  I'm just not sure how to go about it.  The documentation at http://ws.apache.org/axis/java/user-guide.html, is vague on this area.

Any other suggestion?
Since is suspect this is all about serialization ultimately, you could override read/writeObject


private void writeObject(ObjectOutputStream oos)
    throws IOException {
  oos.defaultWriteObject();
  // Write/save additional fields
  oos.writeLong(creationDate.getTime());
}

declaring

private transient Timestamp creationDate;

but this is 'educated guesswork' on my part ;-)
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Objects,

You are correct.  I just got back for the book store.  I picked up a book called, Building Web Services with Java.  I found out two major points:

The temporary solution I found was to use java.util.Date  Unlike the Axis web site, it has a table describing all of the compatible mappings.  java.util.Date happens to be one of them.  It's not in the list on the Axis website (go figure).
 
The permanent fix would be to write a custom Serializer and Deserializer for Timestamp.  On page 285 the before mentioned book, it says the following:

"
1. Write a Serializer implementation that takes an object of your Java type and writes the XML the way you like it using a SerializationContext.

2. Make sure the serializer writes a correct schema for the generated XML.

3. Write a simple factory implementation so that the engine can create instances of you serializer.  Look at any of the seralizer factories in the org.apache.axis.encoding.ser package for examples.  The SerializerFactory you build will be the class you refer to as the serializer component in WSDD type mappings and in the Call.registerTypeMapping() client APIs.
"

The first alternative, using java.util.Date is not the end of the world.  I would simply have to make minor adjustments to my DAOs and VOs.  Conversion between Timestamp and java.util.Date is not that major.
 
For example:
 
java.util.Date dt;
java.sql.Timestamp ts;
 
ts = new Timestamp(dt.getTime());
dt = new java.util.Date(ts.getTime());
 
I think this should work.
 
This may be the best solution because it java.util.Date maps to dateTime, thus making it useable by other none java platforms.
 
Thanks for the confermation.