Solved

Defining Timestamp for use in the WSDL

Posted on 2004-08-02
7
2,414 Views
Last Modified: 2008-03-10
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
0
Comment
Question by:HycelTaylor
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11695710
Simplest would be to use xsd:long:

private long creationDate;


...

creationDate = System.currentTimeMillis();
0
 

Author Comment

by:HycelTaylor
ID: 11696041
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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11696420
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 ;-)
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 11699765
you need to define a custom mapping, specifying serialer/deserializer classes. Something like:

<typeMapping qname="ns:local" xmlns:ns="someNamespace"
    languageSpecificType="java:java.util.Timestamp"
    serializer="au.com.objects.TimestampSerializer"
    deserializer="au.com.objects.TimestampDeserializer"
    encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 11699775
0
 

Author Comment

by:HycelTaylor
ID: 11699887
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 11699916
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question