[英]Polymorphism in JAX-RPC web services
I have a JAX-RPC (Java) web service that needs to return a complex polymorphic value. 我有一个JAX-RPC(Java)Web服务,需要返回一个复杂的多态值。 To be more specific, the class structure is something like this: 更具体地说,类结构是这样的:
abstract class Child {
}
class Question extends Child {
private String name;
// other fields, getters, and setters
}
class Section extends Child {
private String label;
private Child[] children;
// getters and setters
}
class Quiz {
private Child[] elements;
// getter and setter
}
My web service has a method that returns a Quiz, which of course may contain Questions and Sections which may contain Questions and other Sections, and so on and so forth. 我的Web服务具有一种返回测验的方法,该测验当然可以包含问题和部分,其中可能包含问题和其他部分,依此类推。 However, when I generate the WSDL, only Child and Quiz make it in. When I call the web service, I get back a Quiz element with the right number of children, but they're all Child elements, and they're all empty. 但是,当我生成WSDL时,只有Child和Quiz进入其中。当我调用Web服务时,我会返回一个带有正确数量的子代的Quiz元素,但它们都是Child元素,而且都为空。
Is there a good way to make this work, short of just returning XML as a String? 除了仅将XML作为String返回之外,还有什么好方法可以使这项工作有效?
Before anyone asks, due to circumstances beyond my control, I cannot use JAX-WS. 在任何人问到之前,由于无法控制的情况,我无法使用JAX-WS。
Maybe someone is still looking for it, it can be done in axis 1.4: 也许有人还在寻找它,可以在轴1.4中完成:
Add next line into your section of axis web service deployment file (wsdd): 将下一行添加到Axis Web服务部署文件(wsdd)的部分中:
<parameter name="emitAllTypesInWSDL" value="true" />
Modify your task in ant build file to include 'extraClasses': 在ant构建文件中修改您的任务,使其包含“ extraClasses”:
<axis-java2wsdl ... extraClasses="..."></axis-java2wsdl>
In extraClasses mention all classes which will be passed, since axis aren't able to guess which childs you'll be passing/returning as parameters. 在extraClasses中,将提及将要传递的所有类,因为axis无法猜测您将传递/返回哪个孩子作为参数。
Done, now you can pass derivated classes in methods accepts parent classes. 完成后,现在您可以在接受父类的方法中传递派生类。 Etc: 等等:
// server side class A { ...}
class B extends A {...}
class C extends A {...}
// ws
class wsAxis { public void processPolymorphCall(A obj); }
// client side
wsAxis.processPolymorphCall(new C());
// this will work now, as much as returning derivated classes in place of base class.
I don't think JAX-RPC supports polymorphism in that way. 我认为JAX-RPC不以这种方式支持多态。 I had a similar problem, and had to work around it by creating a class that had just two members - one for each of the two classes that could possibly be returned - and only populating one depending on the type I wanted to return. 我有一个类似的问题,因此必须通过创建一个只有两个成员的类来解决该问题-每个可能返回的两个类中的每个成员-并根据我要返回的类型填充一个。 So in your case: 因此,在您的情况下:
class Child
{
private Section section;
private Question question;
// Constructor, etc...
}
class Question
{
private String name;
// other fields, getters, and setters
}
class Section
{
private String label;
private Child[] children;
// getters and setters
}
class Quiz
{
private Child[] elements;
// getter and setter
}
Which requires the client to check which member of child is populated, and is horribly ugly, I know. 我知道,这需要委托人检查哪个儿童成员居住并且丑陋得可怕。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.