Solved

Defining Timestamp for use in the WSDL

Posted on 2004-08-02
7
2,486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

635 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