Spring專案整合WebService服務遇到問題記錄
專案是用SSM+Maven構建,在整合WebService時,將遇到問題解決方式記錄如下:
基於WS風格使用方式 1、所需要類庫: <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.2.4</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.2.4</version> </dependency>
<dependency> <groupId>org.codehaus.woodstox</groupId> <artifactId>stax2-api</artifactId> <version>3.1.4</version> </dependency>
<dependency> <groupId>org.codehaus.woodstox</groupId> <artifactId>woodstox-core-asl</artifactId> <version>4.4.1</version> </dependency>
2、新增cxf.xml檔案
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- Spring整合CXF釋出基於WS的服務配置檔案: address:服務地址 serviceClass:服務的介面 serviceBeans:服務實現類 訪問地址:http://localhost:8080/ws/manuService/ --> <bean id="mywsInterceptor" class="com.modules.webservice.interceptor.MyInterceptor"/> <jaxws:server address="/manuService" serviceClass="com.modules.webservice.service.IManuService"> <jaxws:serviceBean> <bean class="com.modules.webservice.service.impl.ManuServiceImpl"></bean> </jaxws:serviceBean> <jaxws:inInterceptors> <ref bean="mywsInterceptor"></ref> </jaxws:inInterceptors> </jaxws:server> </beans>
3、客戶端動態呼叫wsdl檔案 String operation = "方法名";//呼叫方法名稱 JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance(); Client client = clientFactory.createClient("http://IP:埠/專案名ws/manuService?wsdl"); //解決服務端介面和實現檔案目錄不一致導致的No operation was found with the name異常 Endpoint endpoint = client.getEndpoint(); QName qname = new QName(endpoint.getService().getName().getNamespaceURI(),operation); BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding(); if(bindingInfo.getOperation(qname) == null){ for(BindingOperationInfo operationInfo :bindingInfo.getOperations()){ if(operation.equals(operationInfo.getName().getLocalPart())){ qname = operationInfo.getName(); break; } } } Object[] result = client.invoke(qname,"入參,可為空"); System.out.println(result[0]);
4、遇到問題: a、cxf的類庫與stax2-api類庫和woodstox-core-asl類庫存在版本匹配問題,出現錯誤時:cxf-3.2.6,stax2-api-4.1,woodstox-core-asl-4.4.1。 問題現象是:本地Windows環境和虛擬器Centos環境均正常,部署到阿里雲Centos出現wsdl無法訪問, 訪問wsdl,報500錯誤:java.lang.NoSuchMethodError:org/codehaus/stax2/ri/EmptyIterator。 對於客戶端,錯誤提示資訊是: javax.xml.ws.soap.SOAPFaultException: Cannot create a secure XMLInputFactory。 解決方案: 採用的cxf-3.2.4、stax2-api-3.1.4和woodstox-core-asl-4.4.1組合,可正常使用。