users@jaxb.java.net

Re: mapping using annotations without compiling a schema

From: Dmitri Colebatch <colebatchd_at_gmail.com>
Date: Tue, 21 Jun 2005 10:04:50 +1000

On 6/21/05, Kohsuke Kawaguchi <kohsuke.kawaguchi_at_sun.com> wrote:
> 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...

Thanks - I have no problems hanging in there, I was more concerned of
asking a question that warranted a response of RTFM (o: I'm waiting
on my request for cvs access to come through, so hopefully that'll
make it a bit easier to keep up with what's going on.

> 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.

Hmm, I'm in splashing around in the deep end with annotations, but as
I understand it the default is really
@XmlAccessorType(AccessType.PROPERTY) and what you're suggesting is
@XmlAccessorType(AccessType.NONE) - however I don't see
AccessType.NONE (it only have property and field).

Using XmlTransient as suggested by you and Lexi I managed to make the
following do what I want:

@XmlRootElement(name = "DealMsg", namespace = "urn:deal")
public class Deal
{
        @XmlEnum(value=String.class)
        enum DealStatus
        {
                @XmlEnumValue(value="In progress")
                inProgress,
                @XmlEnumValue(value = "Finished")
                finished
        };

        @XmlElement(name = "DealStatus", namespace = "urn:deal")
        private DealStatus status = DealStatus.inProgress;

        @XmlTransient
        public DealStatus getStatus()
        {
                return status;
        }

        public void setStatus(DealStatus status)
        {
                this.status = status;
        }
}

It spits out

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<DealMsg xmlns="urn:deal">
    <DealStatus>In progress</DealStatus>
</DealMsg>

But it feels dirty. The field is not transient, I'm simply hacking
around my inability to make your suggestion of NONE work. That
suggestion feels much better to me. Is this something that perhaps
has slipped through the cracks, or is it a misunderstanding of mine
with regard to annotations?

Thanks both for your help. Look forward to hearing more about the
@XmlAccessorType(AccessType.NONE) option.

cheers
dim