zoukankan      html  css  js  c++  java
  • WebService -- Java 实现之 CXF (WebService 服务器端接口)

    1. 使用Maven创建一个quickstart项目

    2. 引入依赖的Jar包

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-core</artifactId>
        <version>3.1.5</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.1.5</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-jetty</artifactId>
        <version>3.1.5</version>
    </dependency>
    

    3. 编写Service 接口 + 实现类

    package com.example.tuo.webservice;

    import javax.jws.WebService;

    @WebService
    public interface HelloWorld {

      public String sayHello(String sb);

    }

    package com.example.tuo.webservice.impl;
    
    import javax.jws.WebService;
    
    import com.example.tuo.webservice.HelloWorld;
    
    @WebService
    public class HelloWorldImpl implements HelloWorld{
    
    	public String sayHello(String sb) {
    		// TODO Auto-generated method stub
    		return "Hello world," +sb;
    	}
    
    }
    

    4. 启动服务

    package com.example.tuo.server;
    
    import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
    
    import com.example.tuo.webservice.HelloWorld;
    import com.example.tuo.webservice.impl.HelloWorldImpl;
    
    public class Server {
    
    	public static void main(String[] args){
    		
    		System.out.println("web service starting...");
    		
    		
    		JaxWsServerFactoryBean wsSvrFactoryBean = new JaxWsServerFactoryBean();
    		String address = "http://127.0.0.1/helloWorld";
    		wsSvrFactoryBean.setAddress(address);
    		wsSvrFactoryBean.setServiceClass(HelloWorld.class);
    		HelloWorld implementor = new HelloWorldImpl();
    		wsSvrFactoryBean.setServiceBean(implementor);
    		wsSvrFactoryBean.create();
    		
    		
    		System.out.println("web service started...");
    	}
    }
    

    5. 访问服务

    至此,我们的第一个基于CXF的webservice已经编写完成并发布出来了。

  • 相关阅读:
    Ubuntu中安装gdal python版本
    python中在计算机视觉中的库及基础用法
    Google earth爬取卫星影像数据并进行标注路网的方法
    事务
    文件的下载,随机验证码(无验证)登录注册
    类的加载器和反射
    等待唤醒机制,UDP通信和TCP通信
    线程池,多线程,线程异步,同步和死锁,Lock接口
    多线程, Thread类,Runnable接口
    转换流,缓冲流
  • 原文地址:https://www.cnblogs.com/atuotuo/p/6227830.html
Copyright © 2011-2022 走看看