EJB: Class has two properties of the same name

Error:


Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:
1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "xyz"
this problem is related to the following location:



Cause:


This is because you are having public field(s) inside your class which is exposed through the webservice. And the JAX-WS uses default binding option XmlAccessType.PUBLIC_MEMBER for the webservices. Which means every public members and the public getter/setters will be bound. Lets see the following example:


public class Test{
public String xyz;

public String getXyz(){
}

public void setXyz(){
}
}



Since you have used public field, the JAXB will bind both the public field and the public getter/setter for your webservice. And that's why its always saying Class has two properties of the same name "xyz"(one from field, and one from getter/setter).


Solution:


Best solution is to use the private for the field, instead of using public. I mean your public String xyz should be private String xyz.


If you have no option rather than using public for the field, then another solution is to use the following annotation above the java class to force the JAXB to use only the members and discarding the getter/setters for binding.


@XmlRootElement(name = "test")
@XmlAccessorType(XmlAccessType.FIELD)
public class Test{
...



Comments