The Source for Java Technology Collaboration

Home » java.net Forums » GlassFish » Metro and JAXB

Thread: Why does the JAX-WS client code use XMLGregorianCalendar?

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 9 - Last Post: Oct 24, 2006 5:06 PM by: kohsuke
jemiller1

Posts: 15
Why does the JAX-WS client code use XMLGregorianCalendar?
Posted: Oct 13, 2006 12:10 PM
  Click to reply to this thread Reply

Does anyone know why JAX-WS uses XMLGregorianCalendar for Date values on the client side? Why not just use Date instead? I can see why it would use it internally, but, I can't see why the client should have to use something XML specific. The Java Calendar and Date classes seem unnecessarily complicated to begin with and XMLGregorianCalendar is even worse. From what I know so far, there isn't even a way to initialize an XMLGregorianCalendar from a Date in one step. i.e. There's no setTime() method. You have to set the fields individually which seems rather unnecessary.

.NET just uses the standard DateTime class. I don't know why things have to be more complicated in the Java world.

jemiller1

Posts: 15
Re: Why does the JAX-WS client code use XMLGregorianCalendar?
Posted: Oct 13, 2006 12:20 PM   in response to: jemiller1
  Click to reply to this thread Reply

I just noticed that you can use DatatypeFactory to create an XMLGregorianCalendar and set it to a GregorianCalendar. What's the point of having a separate factory?

kohsuke

Posts: 3,962
Re: Why does the JAX-WS client code use XMLGregorianCalendar?
Posted: Oct 14, 2006 7:23 AM   in response to: jemiller1
  Click to reply to this thread Reply

XMLGregorianCalendar is thought to be necessary because Java's standard Date/Calendar types cannot represent all the possible values allowed in XML Schema date-related types. Such difference is often small --- for example, whether you can correctly represent 0.0001sec, or year 9999999999999, etc. So I kind of agree that it was a poor choice to make that a default.

Then there's a fact that XMLGregorianCalendar is awfully hard to use. The factory class, as you pointed out, then the checked exception it throws. There are a few RFEs filed around this, so it should become better in the next revision of JAXP.

The good news is that you can tell JAXB to use Calendar instead of XMLGregorianCalendar. Check the archive with XMLGregorianCalendar and you'll find how.

skaffman

Posts: 15
Re: Why does the JAX-WS client code use XMLGregorianCalendar?
Posted: Oct 16, 2006 4:58 AM   in response to: kohsuke
  Click to reply to this thread Reply

> The good news is that you can tell JAXB to use
> Calendar instead of XMLGregorianCalendar. Check the
> archive with XMLGregorianCalendar and you'll find how.

Alternatively, use a binding customisation which binds the XSD types to JodaTime types. Much, much nicer to use than XmlGregorianCalendar and GregorianCalendar.

http://joda-time.sourceforge.net/

jemiller1

Posts: 15
Re: Why does the JAX-WS client code use XMLGregorianCalendar?
Posted: Oct 16, 2006 3:17 PM   in response to: kohsuke
  Click to reply to this thread Reply

Hehe, thanks. Yeah, after I wrote this, I noticed that it threw a checked exception. I was grumbling about that to myself and my boss. That's good to know that you can configure the data type. I'll look into that.

On an unrelated note, is the forum supposed to notify you when someone posts a reply and you've selected to watch the post? Maybe it's my spam filters...

kohsuke

Posts: 3,962
Re: Why does the JAX-WS client code use XMLGregorianCalendar?
Posted: Oct 16, 2006 4:48 PM   in response to: jemiller1
  Click to reply to this thread Reply

Another person reported the same issue. I'll forward your comment to admin.

prophecy

Posts: 3
Re: Why does the JAX-WS client code use XMLGregorianCalendar?
Posted: Oct 21, 2006 9:07 PM   in response to: kohsuke
  Click to reply to this thread Reply

I just ran into this same thing, my Date fields on the server end up as XMLGregorianCalendar on the client. Foir something so common as Date, I'm surprised that JAX-WS would do something like this.

Is there any way to make it use java.util.Date in some config or annotation?

kohsuke

Posts: 3,962
Re: Why does the JAX-WS client code use XMLGregorianCalendar?
Posted: Oct 23, 2006 9:16 AM   in response to: prophecy
  Click to reply to this thread Reply

See http://weblogs.java.net/blog/kohsuke/archive/2006/03/how_do_i_map_xs.html

also search the archive.

prophecy

Posts: 3
Re: Why does the JAX-WS client code use XMLGregorianCalendar?
Posted: Oct 23, 2006 10:24 AM   in response to: kohsuke
  Click to reply to this thread Reply

Yes, I know you can customize it, but if my objects have java.util.Date when running wsgen, wouldn't it be reasonable to expect wsgen would take care of that for me? Or am I missing something?

kohsuke

Posts: 3,962
Re: Why does the JAX-WS client code use XMLGregorianCalendar?
Posted: Oct 24, 2006 5:06 PM   in response to: prophecy
  Click to reply to this thread Reply

I think it's not necessarily desirable for there to be such strong tie between how the server is written and how the client is written, so it's hard to say.

Providing such an option in wsgen would certainly be an excellent idea.




 XML java.net RSS