2

抽象クラスを書きました

import javax.xml.bind.annotation.*;
public abstract class Parent
    {
    @XmlAttribute(name = "one")
    public String getOne() { return "one";}
    }

および2つの派生クラス:

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またはChild2の定義は含まれません。2つの具象クラスの定義を生成するようにwsgenに指示する方法はありますか?

ありがとう、

4

1 に答える 1

3

注釈を追加する@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());
    }
}

あなたがここで見つけることができるこの振る舞いについての興味深い情報

于 2012-02-14T20:34:43.993 に答える