zoukankan      html  css  js  c++  java
  • 使用CXF创建Web Services

      Apache的CXF创建Web Services非常简单。

      1、下载CXF发布包,http://cxf.apache.org/download.html ,我这里下载的是apache-cxf-2.7.0.zip

      2、解压发布包。

      3、建立一个Java Project,把\apache-cxf-2.7.0\lib目录中的jar包都引入到项目中。

      4、编写服务器端代码

        1)服务接口

    package cn.luxh.app.ws;
    
    import javax.jws.WebService;
    
    @WebService
    public interface Calculator {
    	
    	/**
    	 * 提供加法服务的接口方法
    	 * @param num1	加数1
    	 * @param num2	加数2
    	 * @return
    	 */
    	int add(int num1,int num2);
    }
    

        2)服务实现类

    package cn.luxh.app.ws;
    
    import javax.jws.WebService;
    
    @WebService(endpointInterface="cn.luxh.app.ws.Calculator",serviceName="Calculator")
    public class CalculatorImpl implements Calculator{
    
    	@Override
    	public int add(int num1, int num2) {
    		return num1 + num2;
    	}
    
    }
    

        其中@WebService在类上注释,表示这个类的方法就变成WebService方法了。属性endpointInterface指定服务的接口,属性serviceName指定服务名称。

      5、发布服务

    package cn.luxh.app.ws;
    
    import javax.xml.ws.Endpoint;
    
    import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
    
    /**
     * 发布服务
     * @author Luxh
     * 2012-11-5
     */
    public class CalculatorServer {
    	
    	/**
    	 * 发布方法一
    	 * 使用Endpoint发布服务
    	 */
    	private static void server1() {
    		System.out.println("Start Server1...");
    		CalculatorImpl implementor = new CalculatorImpl();
    		String address = "http://localhost:8080/app/ws/Calculator";
    		Endpoint.publish(address, implementor);
    	}
    	
    	/**
    	 * 发布方法二
    	 * 使用JaxWsServerFactoryBean创建服务
    	 */
    	private static void server2() {
    		System.out.println("Start Server2...");
    		CalculatorImpl implementor = new CalculatorImpl();
    		//create Server endpoints
    		JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
    		svrFactory.setServiceClass(Calculator.class);//设置提供服务的接口
    		svrFactory.setAddress("http://localhost:8080/app/ws/Calculator");//设置提供服务的地址
    		svrFactory.setServiceBean(implementor);//设置服务接口的实现类
    		svrFactory.create();
    	}
    	
    	
    	
    	public static void main(String[] args) throws InterruptedException {
    		
    		server2();
    		System.out.println("Server ready...");
    		Thread.sleep(5*60*1000);
    		System.out.println("Server exiting...");
    		System.exit(0);
    		
    		
    	}
    }
    

      6、在浏览器访问:http://localhost:8080/app/ws/Calculator?wsdl 出现下面的内容,说明服务发布成功。

      7、编写客户端代码

        1)使用wsimport命令。进入命令行,切换到jdk的bin目录,wsimport命令在bin目录中

        2)执行:wsimport -p cn.luxh.app.ws -keep http://localhost:8080/app/ws/Calculator?wsdl

          -p : 指定包名   -keep :保持生成的文件

        3)、命令执行结束后,会在当前目录下生成客户端代码。

        4)、新建一个Java Project,做为客户端测试,把刚才生成的客户端代码复制到项目中即可。

        5)、测试代码

    package cn.luxh.app.client;
    
    import cn.luxh.app.ws.Calculator;
    import cn.luxh.app.ws.CalculatorService;
    
    public class CXFClient {
    	public static void main(String[] args) {
    		
    		Calculator calculator = new CalculatorService().getCalculatorPort();
    		int sum = calculator.add(1, 2);
    		System.out.println("sum is : "+sum);
    	}
    }
    

        6)、启动服务器端后,执行上述客户端代码即可。

      

      

      

  • 相关阅读:
    BZOJ-1625 宝石手镯 01背包(傻逼题)
    BZOJ-2929 洞穴攀岩 最大流Dinic(傻逼题)
    BZOJ3252: 攻略 可并堆
    二逼平衡树 Tyvj 1730 BZOJ3196 Loj#106
    [Noi2016]区间 BZOJ4653 洛谷P1712 Loj#2086
    [NOIP2014]飞扬的小鸟 D1 T3 loj2500 洛谷P1941
    BZOJ4554: [Tjoi2016&Heoi2016]游戏 luoguP2825 loj2057
    BZOJ 2599: [IOI2011]Race 点分治
    POJ1038 Bugs Integrated, Inc 状压DP+优化
    JLOI2015 城池攻占
  • 原文地址:https://www.cnblogs.com/luxh/p/2756026.html
Copyright © 2011-2022 走看看