java使用xfire搭建webservice服務的過程詳解
前言
以前用的都是 apache 的cxf來搞webservice,今天做項目發(fā)現(xiàn)這個項目用的是 xfire,于是搭一個,寫個demo用一下,在此記錄一下過程。
搭建過程
本文使用的是maven形式的web工程。不知道如何搭建web工程的看上一篇博文。
引入xfire的依賴
<dependency> <groupId>org.codehaus.xfire</groupId> <artifactId>xfire-all</artifactId> <version>1.2.6</version></dependency>
配置web.xml
<servlet> <servlet-name>XFireServlet</servlet-name> <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/xfire/services.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>XFireServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
編寫接口
public interface HelloService { public String helloService();}
編寫實現(xiàn)類
public class HelloServiceImpl implements HelloService { public String helloService() { return '1111'; }}
配置xfire的services的配置文件
該配置文件通過查看源碼,默認在META-INF下的xfire的services.xml。我將目錄放在了resource下,去掉了META-INF。可通過上方的web.xml配置文件的地址。上方有例子。
<?xml version='1.0' encoding='UTF-8'?><beans> <service xmlns='http://xfire.codehaus.org/config/1.0'> <name>webService</name> <namespace>http://jichi.com/</namespace> <serviceClass>com.HelloService</serviceClass> <implementationClass>com.HelloServiceImpl</implementationClass> </service></beans>
編寫一個訪問webservice的方法
public static Object processWsMethod(String url,String methodName,Object ... params){Object result = null;try {if(!url.endsWith('?wsdl')){url += '?wsdl';}URL urls = new URL(url);Client client = new Client(urls);Object[] results = client.invoke(methodName,params);if(results!=null){if(results.length==1)result = results[0];elseresult = results;}} catch (Exception e) {e.printStackTrace();}return result;}
編寫測試方法
@Test public void testMyWebService(){ String aa = (String) WebServiceUtil.processWsMethod('http://localhost:8080/services/webService', 'helloService'); System.out.println(aa); }
結果
控制臺打印111.搭建成功。
到此這篇關于使用xfire搭建webservice服務的過程詳解的文章就介紹到這了,更多相關使用xfire搭建webservice服務內容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章:
1. Python2.6版本pip安裝步驟解析2. python公司內項目對接釘釘審批流程的實現(xiàn)3. python中Ansible模塊的Playbook的具體使用4. Python自動化之定位方法大殺器xpath5. Python本地及虛擬解釋器配置過程解析6. Python 利用flask搭建一個共享服務器的步驟7. 基于python實現(xiàn)matlab filter函數(shù)過程詳解8. Python中Anaconda3 安裝gdal庫的方法9. python自動化測試三部曲之request+django實現(xiàn)接口測試10. Python importlib模塊重載使用方法詳解
