Background: We are developing an application that communicates with several 3rd party webservices. Sadly, one of them has defined a WSDL file using poor naming conventions. The same names are often re-used for a response element, and the complexType that it employs. The code snipped below shows one such occurrence as an example:
<s:element name="Reset_PasswordResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="Reset_PasswordResult" type="tns:ResetPasswordResponse" />
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="ResetPasswordResponse">
<s:complexContent mixed="false">
<s:extension base="tns:BaseResponse" />
</s:complexContent>
</s:complexType>
We use Maven cxf codegen plugin (jaxb/jax-ws) to compile this to Java classes. To avoid the name clashes, we have formerly used the option -AutoNameResolution. However, we have found that this leads to unexpected results, where on some machines one class gets renamed to ResetPasswordResponse2.java, while on other machines the other class is renamed. This makes collaborative development very difficult, and also gives us worries for the future (what if it won't compile correctly on Jenkins at some point?)
Question: I am looking for a way to manually determine how the translation/renaming should take place.
Are jaxb/jax-ws bindings a possible solution? Are there other options?
Check this question and answer:
JAXB Binding for XSD outside WSDL
In short, you can use so-called binding files to customize names.
<jxb:bindings version="2.1" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings schemaLocation="xsdschema.xsd" node="/xs:schema">
<jxb:bindings node="xs:complexType[@name='ResetPasswordResponse']">
<jxb:class name="ResetPasswordResponseType"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
You may be interested in jaxb:nameXmlTransform
:
Issue with JAXB: nameXmlTransform typeName prefix not working
This would allow you to customize type or element naming rules globally:
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema" jaxb:version="2.0">
<jaxb:bindings schemaLocation="schema.xsd" node="/xsd:schema">
<jaxb:schemaBindings>
<jaxb:nameXmlTransform>
<jaxb:typeName suffix="Type"/>
<jaxb:elementName suffix="Element"/>
</jaxb:nameXmlTransform>
</jaxb:schemaBindings>
</jaxb:bindings>
</jaxb:bindings>
Credits go to Blaise Doughan.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With