我写了一个抽象类
import javax.xml.bind.annotation.*;
public abstract class Parent
{
@XmlAttribute(name = "one")
public String getOne() { return "one";}
}
和两个派生类:
import javax.xml.bind.annotation.*;
@XmlRootElement(name="child1")
public class Child1 extends Parent
{
@XmlAttribute(name = "two")
public String getTwo() { return "2";}
}
import javax.xml.bind.annotation.*;
@XmlRootElement(name="child2")
public class Child2 extends Parent
{
@XmlAttribute(name = "three")
public String getThree() { return "3";}
}
和一个@webservice:
import javax.xml.ws.Endpoint;
import javax.jws.*;
import java.util.*;
@WebService(serviceName="MyServerService", name="MyServer")
public class MyServer
{
private int count=0;
@WebResult(name="test")
@WebMethod
public Parent getOne() { return ++count%2==0?new Child1():new Child2();}
public static void main(String[] args) {
Endpoint.publish(
"http://localhost:8080/path",
new MyServer());
}
}
使用 wsgen 生成代码时,生成的 XML 架构仅包含抽象类 Parent 的定义,但不包含 Child1 或子2。有没有办法告诉 wsgen 生成两个具体类的定义?
谢谢
最佳答案
添加注释@XmlSeeAlso
应该可以解决问题:
@XmlSeeAlso({Child1.class, Child2.class})
public abstract class Parent {
@XmlAttribute(name = "one")
public String getOne() {
return "one";
}
}
如果您不想让父类了解其子类,您也可以将该注释放在 WS 级别:
@WebService(serviceName="MyServerService", name="MyServer")
@XmlSeeAlso({Child1.class, Child2.class})
public class MyServer {
private int count=0;
@WebResult(name="test")
@WebMethod
public Parent getOne() {
return ++count%2==0?new Child1():new Child2();
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/path", new MyServer());
}
}
您可以找到有关此行为的有趣信息 here
关于java - wsgen:返回一个抽象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9283755/