Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

IllegalAnnotationException: Class has two properties of the same name

843834Jul 31 2007 — edited Aug 8 2007
Hi,
Could someone explain the strange behaviour of the JAXB 2.0 RI with the following case?
@XmlType
@XmlRootElement
public class Foo {
	@XmlElement
	protected String foo = null;
	public Foo() {
		super();
	}
	public String getFoo() {
		return foo;
	}
	public void setFoo(String foo) {
		this.foo = foo;
	}
}
When marshalling, I get the following error:
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "foo"
	this problem is related to the following location:
		at public java.lang.String test.Foo.getFoo()
		at test.Foo
	this problem is related to the following location:
		at protected java.lang.String test.Foo.foo
		at test.Foo
Changing the visibility of the field has no effects.
Transfering the @XMLElement from the field to the getter solves the problem.

This is just personnal consideration, but I always prefer to annotate fields rather than properties (accessors) when I have the choice, the resulting code looks cleaner for me.
Some of the JAXB annotations (like @XMLElement) are intended to be used with both fields and properties, so... what is the problem?

Thanks
-Jerome
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 5 2007
Added on Jul 31 2007
2 comments
2,628 views