【问题标题】:wsgen: returning an abstract classwsgen:返回一个抽象类
【发布时间】:2012-02-14 20:31:20
【问题描述】:

我写了一个抽象类

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 生成两个具体类的定义?

谢谢,

【问题讨论】:

    标签: java wsdl xsd wsgen


    【解决方案1】:

    添加注释@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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 2019-04-25
      • 2021-07-22
      • 2010-12-13
      • 1970-01-01
      • 2021-11-12
      相关资源
      最近更新 更多