Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jaxb, Class has two properties of the same name

Tags:

java

xml

jaxb

People also ask

What is @XmlAccessorType Xmlaccesstype field?

@XmlAccessorType. Package, Class. Defines the fields and properties of your Java classes that the JAXB engine uses for binding. It has four values: PUBLIC_MEMBER , FIELD , PROPERTY and NONE .

What is XmlAccessorType?

Annotation Type XmlAccessorTypeControls whether fields or Javabean properties are serialized by default. Usage. @XmlAccessorType annotation can be used with the following program elements: package. a top level class.

Is JAXB memory efficient?

Generally JAXB is quite efficient and you shouldn't care about memory issues unless your application handles XMLs of very large size.

What is a JAXB class?

The JAXB compiler generates Java classes that map to constraints in the source XML schema. The classes implements get and set methods that you can use to obtain and specify data for each type of element and attribute in the schema. Process XML documents by instantiating the generated classes in a Java program.


I also faced problem like this and i set this.

@XmlRootElement(name="yourRootElementName")
@XmlAccessorType(XmlAccessType.FIELD)

This will work 100%


You didn't specified what JAXB-IMPL version are you using, but once I had the same problem (with jaxb-impl 2.0.5) and solved it using the annotation at the getter level instead of using it at the member level.


I've also seen some similiar issues like this.

I think, it's because of the place where we use the "@XMLElement" annotation in the (bean) class.

And I think, the 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 and throws the IllegalAnnotationExceptions exception.

Exception Message :

Class has two properties of the same name "timeSeries"

At Getter Method :

    at public java.util.List testjaxp.ModeleREP.getTimeSeries()

At Member Field :

    at protected java.util.List testjaxp.ModeleREP.timeSeries

Solution : Instead of using @XmlElement in the field, use it in the getter method.


There are multiple solutions but basicly if you annotate on variable declaration then you need @XmlAccessorType(XmlAccessType.FIELD), but if you prefer to annotate either a get- or set-method then you don't.

So you can do:

@XmlRootElement(name="MY_CLASS_A")
@XmlAccessorType(XmlAccessType.FIELD)
public class MyClassA
{
    @XmlElement(name = "STATUS")   
    private int status;
   //.. and so on
}

Or:

@XmlRootElement(name="MY_CLASS_A")
public class MyClassA
{
    private int status;

    @XmlElement(name = "STATUS")         
    public int getStatus()
    {
    }
}

just added this to my class

@XmlAccessorType(XmlAccessType.FIELD)

worked like a cham


Your JAXB is looking at both the getTimeSeries() method and the member timeSeries. You don't say which JAXB implementation you're using, or its configuration, but the exception is fairly clear.

at public java.util.List testjaxp.ModeleREP.getTimeSeries()

and

at protected java.util.List testjaxp.ModeleREP.timeSeries

You need to configure you JAXB stuff to use annotations (as per your @XmlElement(name="TimeSeries")) and ignore public methods.


You need to configure class ModeleREP as well with @XmlAccessorType(XmlAccessType.FIELD) as you did with class TimeSeries.

Have al look at OOXS