Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB generated classes serializable with JAX-WS binding

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

like image 284
Bozho Avatar asked Jan 25 '11 09:01

Bozho


2 Answers

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>
    
like image 177
Bozho Avatar answered Nov 06 '22 02:11

Bozho


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());
like image 37
Denian Avatar answered Nov 06 '22 01:11

Denian