我想在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;
}
}
和
@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的对象,但soap消息不发送它类型为Male,因此剥离其“男性”成员,如下所示:
发送时的请求:
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]
答案 0 :(得分:0)
老实说,我很惊讶JAXB在启动时不会抛出异常。您有两种类型定义相同的XmlRootElement。
你应该做的是在类上有@XmlType(name =“Male”),@ XMLType(name =“Female”),@ XMLType(name =“Person”)。这将正确设置TYPE层次结构.I不会在其中任何一个上放置XmlRootElement。让CXF根据@ WebParam等处理它。