Saturday, June 2, 2018

Class has two properties of the same name exception

I think, it's because of the place where we use the "@XMLElement" annotation in the (bean) class. JAXB (annotation processor) considers the member field & getter method of the same field element as different properties, when we use the @XMLElement annotation at the field level it throws the IllegalAnnotationExceptions exception.

Below is the sample exception, which i encountered, when i placed the @XMLElement annotation on the property of my class

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 6 counts of IllegalAnnotationExceptions
Class has two properties of the same name "accountNum"
this problem is related to the following location:
at public java.lang.String com.test.finance.domain.ax.VendTable.getAccountNum()
at com.test.finance.domain.ax.VendTable
this problem is related to the following location:
at private java.lang.String com.test.finance.domain.ax.VendTable.accountNum
at com.test.finance.domain.ax.VendTable



To resolve this issue, JAXB offers a lot of flexibility when interacting with your object model. One area is configuring the use of fields or properties to access the data in your domain objects. This is specified as an XmlAccessType (PUBLIC_MEMBER, PROPERTY, FIELD, or NONE) via the @XmlAccessorType annotation. So, you add the XmlAccessType to your class, as shown below, will resolve the issue.

@XmlAccessorType(XmlAccessType.FIELD)


Happy Programming...!!!

No comments:

Post a Comment