zoukankan      html  css  js  c++  java
  • 使用xfire搭建webservice服务

    前言

    以前用的都是 apache 的cxf来搞webservice,今天做项目发现这个项目用的是 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();
    }
    

    编写实现类

    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];
    				else
    					result = 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.搭建成功。

  • 相关阅读:
    chrome 开发者工具
    iOS-登陆界面 实现光标换行功能
    iOS-UIkit复习和代理的使用实现文本框限制输入字数控制
    iOS-代理模式
    ios-疯狂猜图
    ios-状态栏的改变
    iOS-应用管理 点击按钮下载动画
    ios-利用xib重新写 应用管理
    ios-应用管理 字典转模型
    ios-应用管理
  • 原文地址:https://www.cnblogs.com/jichi/p/11353263.html
Copyright © 2011-2022 走看看