網上的wsdl 說明示例:
有個項目上遇到解析出來都是帶有前綴的,例如:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://services.founder.com"> <soapenv:Header/> <soapenv:Body> <ser:funInterFaceMZBYJ> <ser:in0>?</ser:in0> </ser:funInterFaceMZBYJ> </soapenv:Body> </soapenv:Envelope>
之前一直用的hutool 調用 webservice,都比較好用,但是遇到這種奇怪的格式就一直報命名空間的錯誤。只有換種方式,網上搜了下,親測可用。參考地址:(20條消息) Java調用WebService的幾種方式_世界joker的博客-CSDN博客_java webservice
具體步驟如下
1、引用包
<!--spring web Service的包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> <version>1.5.2.RELEASE</version> </dependency> <!--spring web service wsdl包--> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.3</version> </dependency> <!-- axis 1.4 jar start --> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>commons-discovery</groupId> <artifactId>commons-discovery</artifactId> <version>0.2</version> <exclusions> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis-jaxrpc</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.apache.axis</groupId> <artifactId>axis-saaj</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.4</version> </dependency>
2、執行示例
//wsdl地址 String endpoint ="http://100.100.100.63:8888/founderWebs/services/ICalculateServicePZH"; //命名空間 String namespace = "http://www.w3.org/2001/XMLSchema"; //服務名 String serviceName = "ICalculateServicePZH"; //方法名 String methodName = "funInterFaceMZBYJ"; //soapAction String soapAction = ""; Service service = new Service(); Call call = (Call) service.createCall(); //設置響應超時 call.setTimeout(3000); //設置地址 call.setTargetEndpointAddress(endpoint); //設置方法名 call.setOperationName(new QName(namespace,methodName)); String param = "<funderService functionName='MZBYJ_201'>" + " <value>1</value>" + " <value>"+code+"</value> " + " </funderService>"; //設置參數 call.addParameter("Message", XMLType.XSD_STRING, ParameterMode.IN); String message = param; Object [] obj = {message}; //設置返回類型 call.setReturnType(XMLType.XSD_STRING); //啓用soap call.setUseSOAPAction(true); //設置soapAction call.setSOAPActionURI(soapAction); //設置服務名 SOAPService soapService = new SOAPService(); soapService.setName(serviceName); call.setSOAPService(soapService); String ret = (String) call.invoke(obj);