Spring + JAXB集成:Class有两个同名的属性

时间:2014-06-23 13:10:10

标签: java spring web-services jaxb

我在生成WSDL 时遇到问题。我想创建生成 json string SOAP Web服务。下面的pojo类从另一个web项目引用到我的web服务项目中。我在 servlet响应对象上编写生成的json字符串,该对象是使用 WebServiceContext &它使用@Resource注释进行注释。

我还尝试调试 Web服务方法(使用@WebParam进行参数化注释),pojo但是项目没有在调试模式下启动。在调用Web方法之前,会抛出所有字段的异常

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
...

我正在使用 jaxws-ri-2.2.8 jaxws-json-1.2 jaxws-spring-1.9 xbean-spring-3.9 &的弹簧框架-4.0.3.RELEASE - 距离

注意:我对网络服务不是很熟悉,所以请礼貌和礼貌患者

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
}

参考文件:

我也参考下面的链接来解决错误

JAXB's @XmlAccessorType - @Blaise Doughan

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

IllegalAnnotationsException: Class has two properties of same name - Stackoverflow

我尝试了@Blaise Doughan给出的所有例子。一切正常,在Java应用程序和Web应用程序也是。还尝试生成wsdl&amp;它产生了&amp;在控制台上打印输出xml。

有人能指出我的pojo课程中有什么问题吗?我花了很多时间在这上面,但没有运气。我应该怎么做才能摆脱这个错误?

编辑:

我还在getter方法&amp ;; 上尝试了@XmlTransient 注释所有字段上的 @XmlElement 注释,但同样的问题。

非常感谢

1 个答案:

答案 0 :(得分:9)

默认情况下,JAXB将公共属性(get / set方法对)视为已映射。如果您还注释相应的字段(实例变量),您将获得此异常。

如果使用@XmlAccessorType(XmlAccessType.FIELD)注释您的类,则JAXB会将字段视为已映射。如果您还注释了相应的属性,您将获得此异常。

检查Stacktrace

在堆栈跟踪中,您可以看到JAXB impl抱怨LoginResult.getNotifications()LoginResult.getNotifications()都被映射。

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

了解更多信息

我在博客上写了更多关于JAXB和访问者类型的文章: