【发布时间】:2011-06-01 09:22:01
【问题描述】:
我想在 web 服务方法中发送一个抽象对象作为参数。
这些是我的课程: 抽象类:
@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;
}
}
和
@XmlRootElement(name = "person")
public class Female extends Person{
private boolean female;
/**
* @return the female
*/
public boolean isFemale() {
return female;
}
/**
* @param female the female to set
*/
public void setFemale(boolean female) {
this.female = female;
}
}
这是我的网络服务接口:
@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);
}
我的问题是我想向 webMethod: getIsMale 发送一个男性类型的对象,但是肥皂消息没有发送男性类型的对象,因此剥离了它的“男性”成员,如下所示:
发送时的请求:
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]
【问题讨论】:
-
既然“Person”既可以是男性也可以是女性,那么在 Person 上定义一个“gender”属性不是更好吗,您可以在相应的子类的构造函数中将其设置为男性或女性?这样一来,所有的 Person 都有一个可测试的性别?
标签: web-services soap xml-serialization cxf abstract