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

OO7*_*OO7 1 java spring web-services jaxb

我在生成WSDL时面临问题.我想创建生成json字符串的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
...
Run Code Online (Sandbox Code Playgroud)

我使用的是jaxws-ri-2.2.8,jaxws-json-1.2,jaxws-spring-1.9,xbean-spring-3.9spring-framework-4.0.3.RELEASE-dist.

注意:我对Web服务不是很熟悉,所以请礼貌和耐心.

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
}
Run Code Online (Sandbox Code Playgroud)

提到的文件:

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

JAXB的@XmlAccessorType - @Blaise Doughan

Jaxb,Class有两个同名的属性 - Stackoverflow

IllegalAnnotationsException:Class有两个同名属性 - Stackoverflow等.

我尝试了@Blaise Doughan给出的所有例子.一切都在Java应用程序和Web应用程序中正常工作.还尝试生成wsdl&它在控制台上生成&打印输出xml.

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

编辑:

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

非常感谢

bdo*_*han 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
Run Code Online (Sandbox Code Playgroud)

欲获得更多信息

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