OO7
OO7

Reputation: 2807

Spring + JAXB integration : Class has two properties of the same name

I am facing problem in generating WSDL. I want to create SOAP web service which generate json string. Below pojo classes are referenced from another web project into my web service project. I write generated json string on servlet response object which is created by using WebServiceContext & it is annotated with @Resource annotation.

I also tried to debug web service method (parameterized annotated with @WebParam), pojo but project is not starting in debug mode. Before calling web method it throws exception for all fields :

Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 15 counts of IllegalAnnotationExceptions
Class has two properties of the same name "result"
this problem is related to the following location:
    at public portal.common.ejb.LoginResult portal.common.ejb.LoginResponse.getResult()
    at portal.common.ejb.LoginResponse
this problem is related to the following location:
    at private portal.common.ejb.LoginResult portal.common.ejb.LoginResponse.result
    at portal.common.ejb.LoginResponse
Class has two properties of the same name "userInfo"
this problem is related to the following location:
    at public portal.common.ejb.UserDTO portal.common.ejb.LoginResponse.getUserInfo()
    at portal.common.ejb.LoginResponse
this problem is related to the following location:
    at private portal.common.ejb.UserDTO portal.common.ejb.LoginResponse.userInfo
    at portal.common.ejb.LoginResponse
Class has two properties of the same name "notifications"
this problem is related to the following location:
    at public java.util.List portal.common.ejb.LoginResult.getNotifications()
    at portal.common.ejb.LoginResult
    at private portal.common.ejb.LoginResult portal.common.ejb.LoginResponse.result
    at portal.common.ejb.LoginResponse
this problem is related to the following location:
    at private java.util.List portal.common.ejb.LoginResult.notifications
    at portal.common.ejb.LoginResult
...

I am using jaxws-ri-2.2.8, jaxws-json-1.2, jaxws-spring-1.9, xbean-spring-3.9 & spring-framework-4.0.3.RELEASE-dist.

NOTE : I am not much familiar with web services, so please be polite & patient.

POJO

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class LoginResponse {

    private LoginResult result;
    private UserDTO     userInfo;
    // Getter/setter
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class LoginResult {

    private List<String>    notifications;
    private boolean         success;
    // Getter/setter
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class UserDTO {

    private String  eMail;
    private Integer externalIdentifier;
    private String  firstName;
    private String  lastLoggedIn;
    private String  lastName;
    private String  phone;
    private Integer role;
    private String  status;
    private String  title;
    private int     userId;
    private String  userIdentifier;

    // Getter/setter
}

Refered Doc:

I also refer below link to solve the error

JAXB's @XmlAccessorType - @Blaise Doughan

Jaxb, Class has two properties of the same name - Stackoverflow

IllegalAnnotationsException: Class has two properties of same name - Stackoverflow , etc.

I tried all examples given by @Blaise Doughan. All works fine in Java application & Web app too. Also tried to generate wsdl & it generated & print output xml on console.

Can anybody point out what's wrong in my pojo classes ? I spend so much time on this but no luck. What should I have to do to get off this error ?

EDIT :

I also tried @XmlTransient annotation on getter method & @XmlElement annotation on all fields but, same issue.

Thanks a lot

Upvotes: 3

Views: 12525

Answers (1)

bdoughan
bdoughan

Reputation: 149017

By default JAXB treats the public properties (get/set method pairs) as mapped. If you also annotate the corresponding field (instance variable) you will get this exception.

If you annotate your class with @XmlAccessorType(XmlAccessType.FIELD) then JAXB will treat the fields as mapped. If you also annotate the corresponding property you will get this exception.

Examining the Stacktrace

In the stacktrace you can see that the JAXB impl is complaining about LoginResult.getNotifications() and LoginResult.getNotifications() both being mapped.

Class has two properties of the same name "notifications"
this problem is related to the following location:
    at public java.util.List portal.common.ejb.LoginResult.getNotifications()
    at portal.common.ejb.LoginResult
    at private portal.common.ejb.LoginResult portal.common.ejb.LoginResponse.result
    at portal.common.ejb.LoginResponse
this problem is related to the following location:
    at private java.util.List portal.common.ejb.LoginResult.notifications
    at portal.common.ejb.LoginResult

For More Information

I have written more about JAXB and accessor types on my blog:

Upvotes: 10

Related Questions