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已经编写完成并发布出来了。

  • 相关阅读:
    开源项目之Android StandOut(浮动窗口)
    小智慧7
    安卓学习
    asp.net学习Request和Response的其他属性
    bash中的转义
    POJ 1833 排列
    Django点滴(四)ORM对象存取
    POJ 1681 Painter's Problem
    linux2.6.32在mini2440开发板上移植(21)之WebServer服务器移植
    [gkk传智]static与多态及向下向上转型,及多态调用总结
  • 原文地址:https://www.cnblogs.com/atuotuo/p/6227830.html
Copyright © 2011-2022 走看看