Web services 如何向web服务发送抽象参数?

Web services 如何向web服务发送抽象参数?,web-services,soap,xml-serialization,cxf,abstract,Web Services,Soap,Xml Serialization,Cxf,Abstract,我想在webservice方法中发送一个抽象对象作为参数 这些是我的课程: 抽象类: @XmlSeeAlso({Male.class, Female.class}) public abstract class Person { public String name; public void setName(String name) { this.name = name; } public String getName() {

我想在webservice方法中发送一个抽象对象作为参数

这些是我的课程: 抽象类:

@XmlSeeAlso({Male.class, Female.class})

public abstract class Person {
    public String name;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}
儿童:

@XmlRootElement(name = "person")
public class Male extends Person {

private boolean male;

/**
 * @return the male
 */
public boolean isMale() {
    return male;
}

/**
 * @param male the male to set
 */
public void setMale(boolean male) {
    this.male = male;
}
}

这是我的Web服务界面:

@WebService
public interface wsTest {
    @WebMethod
    int getInt();

    @WebMethod
    String getString(String s);

    @WebMethod
    Map<String, String> getMap(Map<String, String> map);

    @WebMethod
    String getMaleStr(Male male);

    @WebMethod
    String getIsMale(@WebParam(name = "param")Person person);
}
详情如下:

[?xml version="1.0" standalone="yes"?]
[soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"]
  [soap:Body]
    [ns1:getIsMale xmlns:ns1="http://ws.xconnect.com/"]
      [ns1:param]
        [ns2:name xmlns:ns2="http://ws.xconnect.com"]Avi[/ns2:name]
      [/ns1:param]
    [/ns1:getIsMale]
  [/soap:Body]
[/soap:Envelope]

老实说,我很惊讶JAXB在启动时没有抛出异常。有两种类型定义相同的XmlRootElement


您应该做的是拥有@XmlType(name=“Male”)、@XmlType(name=“Female”)、@XmlType(name=“Person)在类上。这将正确设置类型层次结构。我不会在任何一个类上放置XmlRootElement。让CXF根据@WebParam等来处理它。

既然“Person”可以是男性或女性,那么在Person上定义一个“gender”属性不是更好,您可以在相应的子类中将其设置为男性或女性“constructor?这样,所有人都有可测试的性别?我已经删除了XmlRootElements,并按照您的建议将XmlTypes放在它们上面。1.但是客户机不发送任何男性/女性信息,它只发送姓名(因为“Person”父级中只有“name”信息)。(我使用tcp代理来查看客户端实时发送的内容)2.服务器没有得到任何有用的东西-只有null。我的意思是方法:@Override public String getIsMale(Person){return Person.getName();}抛出NullPointerException。你有什么线索可以帮助解决我的两个问题吗?老实说,我不知道。可能需要查看完整的测试用例。你也可以尝试将XmlSeeAllow注释添加到wsTest接口。我不确定这是否会有帮助。听起来客户端确实不知道子类.
Male male   = new Male();
male.name   = "Avi";
male.setMale(true);
System.out.println("Calling: getIsMale...");
System.out.println("Response is: " + ws.getIsMale(male));
[?xml version="1.0" standalone="yes"?]
[soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"]
  [soap:Body]
    [ns1:getIsMale xmlns:ns1="http://ws.xconnect.com/"]
      [ns1:param]
        [ns2:name xmlns:ns2="http://ws.xconnect.com"]Avi[/ns2:name]
      [/ns1:param]
    [/ns1:getIsMale]
  [/soap:Body]
[/soap:Envelope]