JAXB generated classes serializable with JAX-WS bi

2019-04-22 04:10发布

问题:

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)

<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).

See also this thread

So, how exactly to do that

回答1:

I made it work in two ways:

  1. Using a second binding file, which is JAXB-only, as the one Pascal showed in his answer

  2. 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>
    


回答2:

You can implement an XJC plugin to do that:

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:

WsimportOptions wsimportOptions = new WsimportOptions();
wsimportOptions.getSchemaCompiler().getOptions().activePlugins.add(new SerializablePlugin());