天天看点

JAXB “Class has two properties of the same name” issue

Using the JAX-WS RI, if you want to annotate a field as “required” (for example), you can use the following annotation:

@XmlElement(required=true)

When I added this before a field in a class, e.g.:

@XmlElement(required=true)

private String action;

and then ran wsgen to generate the web service, it would always fail with

Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException:

1 counts of IllegalAnnotationExceptions

Class has two properties of the same name "action"However, if you move the annotation to the getter method, e.g.

@XmlElement(required=true)

public String getAction() {

return action;

}

it works fine.

I came across this issue when using the JAX-WS RI, and there’s been some discussion about it on the web. The answer is detailed on this page (if you can get to it, some of those sun.com sites (incuding, annoyingly, jax-ws.dev.java.net) seem to be down or not available a lot of the time! I ended up just looking at the Google cached text of the page…).

In short, the answer is that unless you specify it, the XmlAccessorType will default to PUBLIC_MEMBER, which will cause JAXB to bind public fields and public getter/setters. So a private instance variable won’t be bound unless you change the default using @XmlAccessorType(XmlAccessType.FIELD) on the class. Not sure if I understand the message though - how come wsgen reports two properties with the same name in this case? - but this is a workaround anyway.