使用WSDL从客户端发送到Web服务的对象在Web服务上为空,但在客户端不是,为什么?

时间:2012-05-11 09:09:46

标签: java web-services wsdl eclipselink

我有一个运行EclipseLink的Web服务。我已经使用基于数据库模式的NetBeans生成了一些Java类(“来自数据库的实体类......”)。

我的问题是两个名为Person.java和Group.java的类,它们是由NetBeans基于数据库模式生成的。 Person.java和Group.java是JAR文件“MyLib.jar”

的一部分

在Group.java中,我有这段代码(以及其他代码):

@Column(name = "groupName") 
private String groupName;    
@OneToMany(cascade = CascadeType.ALL, mappedBy = "group")
private Collection<Person> personCollection;

public String setGroupName(String groupName) {
    this.groupName = groupName;
}

public String getGroupName(){
    return groupName;
}

@XmlTransient
public Collection<Person> getPersonCollection() {
    return personCollection;
}

public void setPersonCollection(Collection<Person> personCollection) {
    this.personCollection = personCollection;
}

我有两个不同的应用程序,包括“MyLib.jar”。一个是Web服务本身,另一个是连接到Web服务的客户端。当然,客户端还包括Web服务接口JAR。

在客户端中,我创建了这样的人员集合,然后将组实例发送到Web服务:

Collection<Person> persons = new ArrayList<Person>();
Person person = new Person();
person.setName("Peter");
persons.add(person); 
group.setName("GroupName");
group.setPersonCollection(persons);
System.out.print(persons); // prints the instance and its content
webserviceInstance.setGroup(group);

真正奇怪的是,在Web服务方法setGroup中,集合为空!

@Override
public void setGroup(Group group) {
    System.out.print(group.getGroupName()); // prints "GroupName"
    System.out.print(group.getPersonCollection()); // prints null meaning the instance is null
    ...
}

我根本无法理解为什么集合在Web服务中为空而在客户端中不为空。 我做错了吗?

2 个答案:

答案 0 :(得分:0)

问题是,WebService不直接支持集合你需要实现一个包含对象集合的类,类似于:

public class ListExamenConductor implements List<Examenconductor>{

    private ArrayList<Examenconductor> al = new ArrayList<Examenconductor>();
.... //Need to implements all methods

然后,在webService中,您将所有集合发送到Object,如下所示:

@WebMethod(operationName = "consultarExamenConductor")
    public ListExamenConductor consultarExamenConductor(@WebParam(name = "identificacion") Identificacion identificacion) {
        System.out.println("Consultar examenes, consultado en "+new Date());
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("scrc_proyectoPU");
        EntityManager em = emFactory.createEntityManager();
        ExamenconductorJpaController jpaController  = new ExamenconductorJpaController(em.getTransaction(), emFactory);
        ListExamenConductor  examenes = new ListExamenConductor();
        examenes.addAll(jpaController.consultarExamen(identificacion));
        System.out.println("Consultar antecedente, resultado "+examenes.size() + " en "+new Date());
        return examenes;
    }

工作

答案 1 :(得分:-1)

我有同样的问题,解决方案是创建其他不使用JPA的类,并且使用此类工作WebService,我认为jpa条目的标记与

冲突