Webservice is an awesome technology when we think of interoperable services. Its very simple, many libraries are available there to expose your existing application as a Webservice with very little effort. But still you can find developers complaining about it when it comes to automatic binding happening at client and server side. Here I'll explain one of the such scenario and how we can override JAXB binding at client side. Most of us will be using IDE to create a webservice or a webservice client, some may be using ant or any other technology. Regardless of the building environment, when you create a client by specifying the WSDL url, the building environment will use wsimport ( or any third party library) command to generate the client side stubs. In the client side stub, all the XmlTypes specified in the WSDL will be converted to corresponding Java Data Types.
Rarely we may need to override this binding. For example a xsd:dateTime XmlType is by default bound to XMLGregorianCalendar. In server side it may be a TimeStamp or a Date. Due to this when the XMLGregorianCalendar gets marshalled in the webservice request, it may not be matching the format expected by the server.
To override this JAXB binding at client side, we can customize the binding of XmlTypes to Java Data Type using JAXB customization.
Below are the steps to override JAXB binding at client side
- Create a JAXB customization with below mentioned contents and save it as an xml file.
<?xml version="1.0" encoding="UTF-8"?> <jaxws:bindings node="wsdl:definitions/wsdl:types/xsd:schema" xmlns:jaxws="http://java.sun.com/xml/ns/jaxws" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" wsdlLocation="relative url to wsdl location"> <jaxb:globalBindings> <jaxb:serializable/> <jaxb:javaType name="java.lang.String" xmlType="xsd:dateTime"/> </jaxb:globalBindings> </jaxws:bindings>
- Provide this file as JAXB customization file from Netbeans as follows (Use -b option if you are using wsimport directly).
- Right click on the Webservice Reference in your project and select Edit Webservice Attributes
- Select the WSDL Customization tab from the next window and upload the preveous xml at External Biding Files
Click OK and refresh your WSDL. Now the XmlType dateTime would be created as java.lang.String ( as specified in the xml ) in the client stub instead of XMLGregorianCalendar.