users@jaxb.java.net

RE: [jaxb2] Namespace and other questions

From: Brennan Spies <brennanspies_at_hotmail.com>
Date: Thu, 12 Apr 2007 22:27:12 -0700

Kohsuke,

Regarding the duplicates error, actually, the field itself is private which
is why I was puzzled...would have expected more "JavaBean"-like behavior. Is
this a bug, or am I reading the docs wrong? Perhaps it was the fact that I
(was) annotating some fields with @XmlElement(name="...") simply because the
field and the XML element name did not match, and not annotating others...?
It seems that if a field is annotated, JAXB assumes field-level access even
if the field is private, regardless of the @XmlAccessorType.

I did notice that if I remove the @XmlAccessorType annotation (i.e., going
to the default PUBLIC_MEMBER), JAXB complains that a private field ("name")
marked @XmlTransient (but which does have public get/set methods) is not
specified in the "propOrder" of @XmlType. If I then reinstate
@XmlAccessorType(XmlAccessType.NONE), this problem goes away.

It seems to me how the class-level annotations and the field-level
annotations interact/take precedence is not clear...


-----Original Message-----
From: Kohsuke Kawaguchi [mailto:Kohsuke.Kawaguchi_at_Sun.COM]
Sent: Thursday, April 12, 2007 11:45 AM
To: users_at_jaxb.dev.java.net
Subject: Re: [jaxb2] Namespace and other questions

Spies, Brennan wrote:
> 1) XmlAccessType.PROPERTY or XmlAccessType.PUBLIC_MEMBER give me
errors
> complaining of duplicates...the runtime would complain that I had
duplicates
> such as "location" and "getLocation()". Why? I had to switch to
> XmlAccessType.NONE and annotate each field member individually.

The error message should have gave you pointers to two offending
property definitions. For example, if you set it to PUBLIC_MEMBER and
both the location field and the getLocation() method is public, you'll
end up binding both.

You can put @XmlTransient on one of those to avoid the duplicate.

>
> 2) When marshalling to <websphere> or <remote-api> elements, JAXB
would
> set the default namespace to null and then set that element to the
namespace
> I had declared in the annotation, like
>
>
>
> <ns4:websphere xmlns="" xmlns:ns4="mynamespace">
>
>
>
> ..even though the default namespace for all of the DOM is "mynamespace".
> Playing around with "##default" didn't help.

This happens when some elements known to JAXBContext uses the empty
namespace. Most likely this is because you don't have
@XmlSchema(elementFormDefault=QUALIFIED)

I really regret this default, but the EG felt we should be consistent
with XML Schema, even if that means bug-to-bug consistent.


> 3) The behavior in #2 caused all of the child elements of <websphere>
or
> <remote-api> to have null namespaces. I had to manually set the
'namespace'
> attribute on the individual @XmlElement annotations in order to set the
> namespace correctly. Is there a way to simply do this at the class level?

See above.

> 4) I am not able to customize the namespace prefix using a custom
> NamespacePrefixMapper class (following the example under /examples in the
> distro). It generates "ns4" every time...

Take a look at a sample bundled in the RI.


-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com