Generating the ObjectFactories from the supplied XSD’s is simple in maven just add the following snippet to the your maven pom file:
01.<plugin>02.<groupId>org.codehaus.mojo</groupId>03.<artifactId>jaxb2-maven-plugin</artifactId>04.<version>1.6</version>05.<executions>06.<execution>07.<id>xjc</id>08.<goals>09.<goal>xjc</goal>10.</goals>11.</execution>12.</executions>13.<configuration>14.<bindingFiles>15.bindings.xjb16.</bindingFiles>17.</configuration>18.</plugin>Next the definition of the JAX-WS services. On the Internet there are a lot of guides available to get started with JAX-WS. No point in getting into that. I found that information on dealing with headers is mixed.
- https://metro.java.net/nonav/1.2/guide/SOAP_headers.html
Solution depends on the com.sun.xml.ws.developer classes. Not where I want to go.
- http://www-01.ibm.com/support/knowledgecenter/SS7K4U_8.5.5/com.ibm.websphere.nd.multiplatform.doc/ae/twbs_cookiejaxws.html
Suggests a solution where the headers are set separately. Does not even answer how to do inbound headers. - http://stackoverflow.com/questions/830691/how-do-i-add-a-soap-header-using-java-jax-ws
Just a sample of the answers that are available on stackoverflow. The solution provided here is the most common.
Setting the header field to true, will add this to the header part of the soap message. the mode flag allows the following values:
- IN
Inbound - OUT
Outbound - INOUT
In and Outbound.
When using the WebParam.Mode.INOUT value changes are high that the container you are running is not able to generate the WSDL and XSD files. The error you get is ambiguous. End result your WSDL is not generated hence the service is not available. The key when using the INOUT mode is the generic class javax.xml.ws.Holder<T>. When the parameter is placed inside this type. The WSDL is generated correctly.
Resulting @WebMethod method:
01.@WebMethod(02.action = "ServiceName",03.operationName = "operation"04.)05.public MyResponse operation(06.@WebParam(mode = WebParam.Mode.INOUT, header = true, name = "JaxbHeader",07.targetNamespace = "http://www.elucidator.nl/header/v1.0")08.final Holder headerHolder,09.@WebParam(mode = WebParam.Mode.IN, name = "data") final JaxbPayload payload)10.throws MyServiceException11.{12.//SNIP13.if (headerHolder.value.flag) {14.headerHolder.value.field = "SomeValue";15.return responseObject;16.}The generated WSDL will be:
01.<definitions xmlns:ns1="..." xmlns:tns="..."02.xmlns:xsd="http://www.w3.org/2001/XMLSchema"03.xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"04.xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata"05.name="MyService"06.targetNamespace="..."07.xmlns="http://schemas.xmlsoap.org/wsdl/">08.<types>09.//SNIP10.</types>11.<message name="operation">12.<part name="parameters" element="tns:operation"/>13.<part name="jaxbHeader" element="ns1:JaxbHeader"/>14.</message>15.<message name="insertResponse">16.<part name="result" element="tns:operationResponse"/>17.<part name="JaxbHeader" element="ns1:JaxbHeader"/>18.</message>19.<portType name="ServiceName">20.<operation name="insert" parameterOrder="parameters JaxbHeader">21.<input message="tns:operation" wsam:Action="operation"/>22.<output message="tns:operationResponse" wsam:Action="http://www.elucidator.nl/ws/v1.0/operationResponse"/>23.</operation>24.</portType>25.<binding name="ServiceNameBinding" type="tns:ServiceName">26.<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>27.<operation name="insert">28.<soap:operation soapAction="operation"/>29.<input>30.<soap:body parts="parameters" use="literal"/>31.<soap:header message="tns:operation" part="JaxbHeader" use="literal"/>32.</input>33.<output>34.<soap:body parts="result" use="literal"/>35.<soap:header message="tns:operationResponse" part="JaxbHeader" use="literal"/>36.</output>37.</operation>38.</binding>39.<service name="ServiceName">40.<port name="ServiceNamePort" binding="tns:ServicePortBinding">41.<soap:address location="http://localhost:9081/Service"/>42.</port>43.</service>44.</definitions>There is however 1 minor disadvantage with this solution. When an exception is thrown you do not have access to the headers anymore. I have a solution for this, implementation of a Handler that intercepts the request, stores the header and modifies the fault response in the handleFault method. But that is a subject for an other post.