java JAXB 生成的可使用 JAX-WS 绑定序列化的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4791891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JAXB generated classes serializable with JAX-WS binding
提问by Bozho
Having JAXB-RI and CXF. WSDL first. I want a generated class of mine to implement Serializable
. I now have the following binding xml, which works (the SEI class name gets changed)
拥有 JAXB-RI 和 CXF。首先是 WSDL。我想要一个生成的类来实现Serializable
. 我现在有以下绑定 xml,它可以工作(SEI 类名已更改)
<jaxws:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema" ...>
<bindings node="wsdl:definitions/wsdl:portType[@name='Foo']">
<!-- change the generated SEI class -->
<class name="IFooService" />
</bindings>
</jaxws:bindings>
No, in this context, where and what should I add. I tried:
不,在这种情况下,我应该在哪里添加和添加什么。我试过:
<xsd:annotation>
<xsd:appinfo>
<jaxb:globalBindings>
<xjc:serializable uid="12343" />
</jaxb:globalBindings>
</xsd:appinfo>
</xsd:annotation>
and
和
<jxb:globalBindings>
<jxb:serializable/>
</jxb:globalBindings>
both inside and outside the <bindings>
tag - either Serializable
is not added, or classes are not generated at all (without any error).
<bindings>
标签内部和外部- 要么Serializable
不添加,要么根本不生成类(没有任何错误)。
See also this thread
另请参阅此线程
So, how exactly to do that
那么,究竟如何做到这一点
采纳答案by Bozho
I made it work in two ways:
我让它以两种方式工作:
Using a second binding file, which is JAXB-only, as the one Pascal showed in his answer
By specifying another
<bindings>
tag that handles the whole namespace:<bindings node="wsdl:definitions/wsdl:types/xsd:schema[@targetNamespace='http://www.yoursite.com/services/mynamespace']"> <jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <jxb:serializable /> </jxb:globalBindings> </bindings>
使用第二个绑定文件,该文件仅限 JAXB,如 Pascal 在他的回答中所示
通过指定
<bindings>
处理整个命名空间的另一个标签:<bindings node="wsdl:definitions/wsdl:types/xsd:schema[@targetNamespace='http://www.yoursite.com/services/mynamespace']"> <jxb:globalBindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <jxb:serializable /> </jxb:globalBindings> </bindings>
回答by Denian
You can implement an XJC plugin to do that:
你可以实现一个 XJC 插件来做到这一点:
public class SerializablePlugin extends Plugin
{
@Override
public boolean run(Outline outline, Options options, ErrorHandler errorHandler) throws SAXException
{
for (ClassOutline classOutline : outline.getClasses())
{
JDefinedClass definedClass = classOutline.implClass;
definedClass._implements(codeModel.ref(Serializable.class));
}
return true;
}
...
}
Then, you can add the plugin to the SchemaCompiler options:
然后,您可以将插件添加到 SchemaCompiler 选项:
WsimportOptions wsimportOptions = new WsimportOptions();
wsimportOptions.getSchemaCompiler().getOptions().activePlugins.add(new SerializablePlugin());