【问题标题】:Spring + JAXB integration : Class has two properties of the same nameSpring + JAXB 集成:类有两个同名属性
【发布时间】:2014-08-13 13:05:59
【问题描述】:

我在生成 WSDL 时遇到问题。我想创建生成 json 字符串SOAP Web 服务。下面的 pojo 类是从另一个 Web 项目引用到我的 Web 服务项目中的。我在 servlet response 对象上编写生成的 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.8jaxws-json-1.2jaxws-spring-1.9 xbean-spring-3.9 & spring-framework-4.0.3.RELEASE-dist.

注意:我对网络服务不太熟悉,所以请保持礼貌和耐心。

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 并在控制台上生成并打印输出 xml。

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

编辑:

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

非常感谢

【问题讨论】:

  • 如果注释 XmlAccessorType(XmlAccessType.FIELD) 怎么办?
  • 注释 XmlAccessorType 注释时出现同样的问题。

标签: java spring web-services jaxb


【解决方案1】:

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

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

检查堆栈跟踪

在堆栈跟踪中,您可以看到 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 和访问器类型的文章:

【讨论】:

  • 感谢您回复@Blaise Doughan。我从 LoginResult 和 UserDTO 类中删除了 XmlRootElement XmlAccessorType(XmlAccessType.FIELD) 注释。它现在工作正常,并且生成了我的 WSDL。我很高兴看到 WSDL。非常感谢。
猜你喜欢
  • 2011-10-09
  • 2012-09-05
  • 1970-01-01
  • 2012-01-27
  • 2020-12-15
  • 1970-01-01
  • 2018-01-01
  • 2014-10-28
  • 2013-02-12
相关资源
最近更新 更多