當我們使用CXF動態客戶端調用WebService接口容易出現如下問題:命名空間問題
Exception in thread "main" org.apache.cxf.common.i18n.UncheckedException: No operation was found with the name {http://impl.service.jws/}sum. at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:289) at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:283) at cxf.bootstrap.CxfDynamicClientOnJwsRtWeb.main(CxfDynamicClientOnJwsRtWeb.java:36)
這個問題是由這個問題因為SIB和SEI類的targetNamespace統一導致的。
解決辦法:
SIB的targetNamespace的命名空間為SEI對應的命名空間targetNamespace相同即可。
SEI (interface接口類):
package jws.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import entity.User; /** * service endpoint interface(SEI) * RPC接口 * 如果返回結果時實例類,則targetNamespace必須用http://service.jws/,即http://+SEI倒序報名+/ * 沒有的話可以,targetNamespace可任意指定http://www.donald.service/jws_service/ * @author donald * 2017年7月7日 * 下午5:11:53 */ @WebService( targetNamespace = "http://service.jws/" // targetNamespace = "http://www.donald.service/jws_service/" ) public interface JwsIService { //@WebMethod注解可寫可不寫 // @WebMethod @WebResult(name="sumResult") public int sum(@WebParam(name="firstNum")int firstNum, @WebParam(name="secondNum")int secondNum); }
SBI (implements接口實現類):
package jws.service.impl; import javax.jws.WebService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import entity.User; import jws.service.JwsIService; /** * Service implementation Bean(SIB) * RPC接口實現 * 注意這里的targetNamespace的命名空間為SEI對應的命名空間,注意最后要加/ * 否則利用CXF動態客戶端調用時,會找不到 * Exception in thread "main" org.apache.cxf.common.i18n.UncheckedException: * No operation was found with the name {http://impl.service.jws/}sum. * @author donald * 2017年7月7日 * 下午5:11:49 */ @WebService(endpointInterface="jws.service.JwsIService", serviceName = "jwsService", portName = "jwsPort", targetNamespace = "http://service.jws/" // targetNamespace = "http://www.donald.service/jws_service/" ) public class JwsServiceImpl implements JwsIService { private static final Logger log = LoggerFactory.getLogger(JwsServiceImpl.class); @Override public int sum(int firstNum, int secondNum) { int result = firstNum+secondNum; log.info("======"+firstNum+"+"+secondNum+"="+result); return result; } }
動態客戶端:
package cxf.bootstrap; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.apache.cxf.transport.http.HTTPConduit; import org.apache.cxf.transports.http.configuration.HTTPClientPolicy; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import jws.service.User; import util.JsonUtil; /** * CXF 動態代理模式,不用生成本地WS代理類, * 通過反射調用 WS 的對應的方法,傳入相應的參數 * 訪問cxf-server-web項目下的webservice; * 測試jaxws-rt發布WebService web方式。 * 此測試實例,用於測試SEI和SIB的targetNamespace指定的webService接口: * http://localhost:8080/cxf_server_web/jws_services?wsdl; * @author donald * 2017年7月8日 * 下午7:24:12 */ public class CxfDynamicClientOnJwsRtWeb { private static final Logger log = LoggerFactory.getLogger(CxfClient.class); private static final String JWS_RT_WSDL_URI = "http://localhost:8080/cxf_server_web/jws_services?wsdl"; public static void main(String[] args) throws Exception { log.info("======CXF-WS Dynamic Client start!======"); JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient(JWS_RT_WSDL_URI); HTTPConduit conduit = (HTTPConduit)client.getConduit(); HTTPClientPolicy policy = new HTTPClientPolicy(); policy.setConnectionTimeout(10000); policy.setAllowChunking(false); policy.setReceiveTimeout(10000); conduit.setClient(policy); Object[] invokeResult = client.invoke("sum", 17,8); log.info("=======sumResult:" + invokeResult[0]); } }
文章轉載至:https://blog.csdn.net/Donald_Draper/article/details/88307382