users@jaxb.java.net

Re: mapping using annotations without compiling a schema

From: Kohsuke Kawaguchi <kohsuke.kawaguchi_at_sun.com>
Date: Mon, 20 Jun 2005 08:11:08 -0700

Dmitri Colebatch wrote:
> Hi all,
>
> I'm sure what I want to do is possible, but I'm struggling finding the
> documentation on the subject. My situation is this - I have a bunch
> of classes that roughly map to a schema. The classes and the schema
> have been developed separately, but with knowledge of each other.
> I've been given a list of what field needs to map to what element, so
> that work is all done. What I'd like to do is just add annotation to
> the existing classes so they are marshalled out to the appropriate
> elements.
>
> My problem is that all the doco pretty well assumes that you are
> compiling a schema (see figure 1-1 in
> http://java.sun.com/webservices/docs/1.1/tutorial/doc/IntroWS6.html).
> Because I dont want to do this I'm struggling to see how all the
> annoations fit together. I'm reading the javadoc on the annotations
> package, which is helpful, but I think what I'm missing is a good
> guide on the default behaviour, so I then know what I need to
> customize.
>
> For example, I've got a little class like this:
>
> @XmlRootElement(name = "DealMsg", namespace = "urn:deal")
> public class Deal
> {
> @XmlElement(name = "DealStatus", namespace = "urn:deal", type = String.class)
> private String status = "In progress";
>
> }
>
> which goes nicely into
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <DealMsg xmlns="urn:deal">
> <DealStatus>In progress</DealStatus>
> </DealMsg>
>
> but when I add my getStatus/setStatus methods I get
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <ns2:DealMsg xmlns:ns2="urn:deal">
> <ns2:DealStatus>In progress</ns2:DealStatus>
> <status>In progress</status>
> </ns2:DealMsg>
>
> which isn't what I want.
>
> Could someone point me at the appropriate source of doco for doing
> this sort of thing? I'll admit to not having read the spec, but I
> have tried to find relevant parts without too much success. Is it
> possible to do this without changing the schema?

I agree that a good tutorial of how to annotate your own class is
necessary. I'm sure we'll have something by the first release, but until
then hang in there...

By default, there's @XmlAccessorType(PUBLIC) assumed on each package. So
that's why you are seeing a lot of unwanted elements in your XML. Try to
set it to @XmlAccessorType(NONE) to have no default, or use @XmlTransient.

When I did a similar thing, @XmlAccessorType(NONE) suited me better,
because in that way only annotated fields get mapped to XML.


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com