Java. Validate SOAP message against a WSDL


If you are creating a webservice client using a code generator based on Apache CXF or Apache Axis 2 , Chances are high that your webservice code should work just fine as long as both the webservice provider and your client are in the same version of SOAP/WS* standards .Based on the API you choose to invoke the webservice you can see whether the logging capability of that API can be used to print out the SOAP request generated .

Another approach could be to use a tool like SOAPUI. You could try to setup a mock webservice using SOAP UI based on the WSDL you have and then call test your webservice client by calling the mock service . See SOAP UI Link here


I need to validate a SOAP message against a WSDL? (in the same way that an XML file can be validated against a XSD schema).

I am not calling any webservice; I just have a SOAP message and a WSDL, and I need to verify that the SOAP message is correct. without calling the webservice or whatsoever afterwards.

I need to make this validate within a Java program. Do you know of a small Java library to do this?

ps: I am aware that several JAX-WS libraries can validate the request/response when you call a webservice. But again, I am not calling any webservice; I have a simple SOAP message, and a WSDL, and I a need a function that validates the SOAP message against the WSDL.

ps: I am also aware that there tools that can do this, such as SOAPUI and XMLSpy. Again, I need to do this validation within my Java program.

ps: I am aware that I could extract the body part of the SOAP message, and validate it against the XSD. However, I'd like to validate the entire SOAP message against the WSDL.

Try this

  1. Use the WSDL and create a Jax-RPC proxy
  2. Create a webservice JAX RPC handler and register it to the webservice reference Please refer this link to see howto create webservice handlers
  3. In the handler, try the below code in the public boolean handleRequest(MessageContext mc) add these lines

    mc.setProperty("USE_TEST_FILE", <your response in .xml file>);
    return false;
  4. In the public boolean handleResponse(MessageContext mc)

    String testFileName = (String) mc.getProperty("USE_TEST_FILE");
    InputStream instream = <read file as an input stream>
    SOAPMessageContext smc = (SOAPMessageContext) mc;
    SOAPMessage message = smc.getMessage();
    SOAPPart soapPart = message.getSOAPPart();
    soapPart.setContent(new StreamSource(instream));
    return false;

now run your proxy.. the proxy will fail if the message is not valid per WSDL

You can create a stub using the WSDL you have and then make the query to the stub using your XML request. If this goes properly your request is correct.

You can import the WSDL in your project and then make a call to the stub service using your XML. This way you can make changes to the validations without any change in the code.