LazyWriter
9 years agoNew Contributor
How to validate the SOAP Response against WSDL?
Hi everybody,
I built a web service based on the responses messages created by SOAPUI. It runs OK but I got a request to validate every SOAP Request and Response recently. My webservice can get the request and generate response. wsdl file is also available. So I have 3 String variables to store the Request, Response and the WSDL.
Below is the method I used for validate requet messages against the wsdl file:
private boolean validateRequestMessage(String message){ // Get operation name from the message RequestObject reqObj = new RequestObject(); reqObj.setRequest(message); String opName = reqObj.getOperation(); // Wsdl file's location String wsdlUrl = responseDir + "/" + "mywsdl.wsdl"; try{ WsdlProject project = new WsdlProject(); // Import the WSDL file WsdlInterface[] wsdls = WsdlImporter.importWsdl(project, wsdlUrl); WsdlInterface wsdl = wsdls[0]; WsdlRequest request = null; String name = ""; for(Operation operation : wsdl.getOperationList()){ WsdlOperation op = (WsdlOperation) operation; switch(op.getName()){ case "OP1": name = "op1-request"; break; case "OP2": name = "op2-request"; break; case "OP3": name = "op3-request"; break; case "OP4": name = "op4-request"; break; default: name = ""; break; } if(opName.equals(name)){ request = op.addNewRequest(opName); WsdlValidator validator = new WsdlValidator((WsdlContext) (operation.getInterface()).getDefinitionContext()); WsdlResponseMessageExchange wsdlResponseMessageExchange = new WsdlResponseMessageExchange(request); wsdlResponseMessageExchange.setRequestContent(message); ValidationError[] result = validator.assertRequest(wsdlResponseMessageExchange, false); if(result.length>0){ for(int i=0;i<result.length;i++){ System.out.println("WSDL Validation Error:" + result[i].toString()); } return false; } return true; } } }catch(Exception e){ e.printStackTrace(); } return false; }
It works with the request validation, but does not work for the response validation. Because the WsdlResponseMessageExchange object only have setRequestContext() method but don't have semilar method to set reponse with String parameter. Is there any other way to make assertResponse() work with response message?
Thanks,
Alex