Spring + JAXB集成:Class有两个同名的属性 [英] Spring + JAXB integration : Class has two properties of the same name

查看:192
本文介绍了Spring + JAXB集成:Class有两个同名的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

 引起:com.sun.xml .bind.v2.runtime.IllegalAnnotationsException:15个IllegalAnnotationExceptions计数
类有两个同名的属性result
此问题与以下位置有关:
at public portal.common .ejb.LoginResult portal.common.ejb.LoginResponse.getResult()
at portal.common.ejb.LoginResponse
此问题与以下位置有关:私人portal.common上的
。 ejb.LoginResult portal.common.ejb.LoginResponse.result
at portal.common.ejb.LoginResponse
类有两个同名的属性userInfo
此问题与以下内容有关location:
at public portal.common.ejb.UserDTO portal.common.ejb.LoginResponse.getUserInfo()
at portal.common.ejb.LoginResponse
此问题与以下位置有关:私人portal.common.ejb.Us上的
erDTO portal.common.ejb.LoginResponse.userInfo
at portal.common.ejb.LoginResponse
类有两个同名的属性notifications
此问题与以下位置有关:
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
此问题与以下位置有关:
私有java.util.List portal.common.ejb.LoginResult。通知
来自portal.common.ejb.LoginResult
...

I我正在使用 jaxws-ri-2.2.8 jaxws-json-1.2 jaxws-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;
私人UserDTO userInfo;
// Getter / setter
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
公共类LoginResult {

private List< String>通知;
私有布尔成功;
// Getter / setter
}

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
公共类UserDTO {

private String eMail;
private Integer externalIdentifier;
private String firstName;
private String lastLoggedIn;
private String lastName;
private String phone;
private整数角色;
私有字符串状态;
private String title;
private int userId;
private String userIdentifier;

// Getter / setter
}

参考文件:



我也在下面的链接中提到解决错误



JAXB的@XmlAccessorType - @Blaise Doughan



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



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



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



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



编辑:



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



非常感谢

解决方案

默认情况下,JAXB将公共属性(get / set方法对)视为映射。如果你也注释相应的字段(实例变量),你将得到这个例外。



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



检查Stacktrace



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

 类有两个同名的属性notifications
这个问题是相关的到以下位置:
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
此问题与以下位置有关:
私有java.util.List portal.common .ejb.LoginResult.notifications
at portal.common.ejb.LoginResult

更多信息



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




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

解决方案

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:

这篇关于Spring + JAXB集成:Class有两个同名的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆