Wednesday, July 22, 2009

Tips and hints when using Spring Web Services

Some things I noted while trying to set up a Spring web services application, that obeys to WS-Addressing and SoapAction headers.


  • Bottom-up approach:

    • Write WSDL and XSD, adding soapAction descriptors and enabling WS-Addressing (see below)

    • Use JAXB to generate beans

    • Set up Spring WS configuration: marshaller, interceptors, endpoint and endpoint mapping

    • Create the endpoint class (@endpoint) and start populating it with web methods, making them match with the WSDL operations, adding annotations @Action and @SoapAction for resp. SoapAction and WS-Addressing endpoints. Use the generated beans for input/output




  • When calling the application, if an "argument type mismatch" SoapFault is returned:

    • logically, check if you're calling the correct Action

    • sometimes it is/isn't needed to nest arguments/return parameters in a JAXBElement; if it isn't needed, this error is returned



  • To make WS-Addressing work

    • add the WSAW namespace in the wsdl: xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl"

    • in the same WSDL, add >wsaw:UsingAddressing wsdl:required="true"/< in the binding part. It's not needed to define Actions in the WSDL, IF you're already defining a SoapAction: as a default this one will be used for the WS-Addressing too.

    • using Spring WS annotations, add @org.springframework.ws.server.endpoint.annotation.Endpoint at the beginning of the class to declare the class as an endpoint; and for each web method, add @org.springframework.ws.soap.addressing.server.annotation.Action and
      @org.springframework.ws.soap.server.endpoint.annotation.SoapAction
      annotations on top of them, ex.

      @SoapAction(Constants.SOAP_LOCATION_URL_CONSULTA_CONTRATO)
      @Action(value=Constants.SOAP_LOCATION_URL_CONSULTA_CONTRATO)
      public JAXBElement consultaContrato(final ConsultaContrato consultaContrato) {
      ...
      }