Fixing “Class has two properties of the same name” exception

I started to learn JAXB from Vogella tutorial site. I changed the package and project name, and running the example generated following exception.

Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "bookList"
	this problem is related to the following location:
		at public java.util.ArrayList com.xmlservice.jaxb.model.Bookstore.getBookList()
		at com.xmlservice.jaxb.model.Bookstore
	this problem is related to the following location:
		at private java.util.ArrayList com.xmlservice.jaxb.model.Bookstore.bookList
		at com.xmlservice.jaxb.model.Bookstore

	at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
	at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source)
	at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
	at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
	at javax.xml.bind.ContextFinder.find(Unknown Source)
	at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
	at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
	at com.xmlservice.jaxb.model.BookMain.main(BookMain.java:43)

Solution: After some googling I figured out, that I have to move annotation to the getter method. That solves above exception.

The Bookstore.java will be look like this:

package com.xmlservice.jaxb.model;

import java.util.ArrayList;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(namespace = "com.xmlservice.jaxb.model")
public class Bookstore {

	private ArrayList<Book> bookList;
	private String name;
	private String location;

	@XmlElementWrapper(name = "bookList")
	@XmlElement(name = "book")
	public ArrayList<Book> getBookList() {
		return bookList;
	}
	public void setBookList(ArrayList<Book> bookList) {
		this.bookList = bookList;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
}

You May Also Like

3 Comments

  1. Hi Thair,

    What you have mentioned is one way to avoid the exception.
    In general, JAXB looks for public accessor methods. If you are going to place the annotation on a field, make sure that you mention the access type as this @XmlAccessorType(XmlAccessType.FIELD).

    I had faced a similar issue while developing RESTFul services in my local environment.

    Happy coding.

    ~cheers,

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.