WebService体系之——HelloWorld搭建

WebService体系之——HelloWorld搭建

 

        摘要:此笔记是使用CXF实现的一个HelloWorld程序的搭建、包括在同一项目中通过模拟客户端访问发布的webservice和不同项目对webservice的服务的访问。使用的CXF是apache-cxf-2.7.10.zip版本的。

 

一:简介

 

        通过阅读前面的两个概念性的文章之后、相信此时已经对webservice有了一定的了解、下面就通过示例的形式来具体、形象的体会一下webservice的作用、及实现。

        主要搭建的步骤:

        1、            到apache的cxf官网上下载相应的jar包。

        2、            建立一个java工程、将下载的jar包引入到项目中。

        3、            创建服务端功能接口。

        4、            创建实现服务端功能接口的具体类。

        5、            发布服务接口。

        6、            创建测试类、充当客户端调用服务端提供的功能、获取服务调用服务端提供的服务(具体点就是调用服务端提供的方法)。

 

二:同一个项目时具体步骤及代码


        1、创建java项目、引入jar包、方便起见就直接把jar包(除endorsed文件夹下的之外)全部引入即可下载之后的包中我们还可以发现有自带的spring的包、这样我们在使用spring集成它的时候就方便很多了、这里先不提集成。后面会补充jar和项目结构图。

        2、创建服务端接口:注意别忘了在服务端接口类级别上加上@WebService ! HelloWebService代码:


package com.chy.ws.service;

import javax.jws.WebService;

@WebService
public interface HelloWebService {

	public String sayHello(String name);
}

        3、创建实现服务端功能接口的具体类——HelloWebServiceImpl代码:


package com.chy.ws.service;

import javax.jws.WebService;

@WebService(endpointInterface="com.chy.ws.service.HelloWebService")
public class HelloWebServiceImpl implements HelloWebService{

	public String sayHello(String name) {
		return "hello " + name;
	}
}

        4、发布服务接口——WebServiceServer代码:


package com.chy.ws.server;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.chy.ws.service.HelloWebService;
import com.chy.ws.service.HelloWebServiceImpl;

public class WebServiceServer {
	
	public WebServiceServer(){
		
		//create an web service interface
		JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
		
		//release the web service
		factory.setAddress("http://localhost:8080/hellowebservice");
		
		//register the web service
		factory.setServiceClass(HelloWebService.class);
		factory.setServiceBean(new HelloWebServiceImpl());
		
		//create the service
		factory.create();
		
		//use another method to start web service
		//Endpoint.publish("http://localhost:8080/hellowebservice", new HelloWebServiceImpl());
	}
	
	public static void main(String[] args) {
		new WebServiceServer();
		System.out.println("server is ready...");
		try {
			Thread.sleep(1000*300);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("server exit...");
		System.exit(0);
	}
}

        5、通过浏览器访问地址:http://localhost:8080/hellowebservice?wsdl    若有结果则发布成功!

        6、测试类——WebServiceClient代码(先将服务端启动或者发布):


package com.chy.ws.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.chy.ws.service.HelloWebService;

public class WebServiceClient {
	public static void main(String[] args) {
		//create a web service proxy factory
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		
		//set the address of web service
		factory.setAddress("http://localhost:8080/hellowebservice");
		
		//register the web service interface
		factory.setServiceClass(HelloWebService.class);
		
		//get the service bean
		HelloWebService hello = (HelloWebService) factory.create();
		
		//check the result
		System.out.println("invoking service...");
		System.out.println(hello.sayHello("andyChen"));
	}
}

 

三:不同项目时客户端搭建具体步骤及代码


        1、将上面实现的服务端搭建好。

        2、创建一个java项目、引入与服务端相同的jar包。

        3、创建服务端功能接口(直接将服务端的java代码复制到客户端、注意:完整路径名一定要一样、及包名完全相同)。

        4、创建测试类来测试服务——Client代码(先将服务端启动或者发布):


package com.chy.ws.client;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.chy.ws.service.HelloWebService;

public class Client {
	public static void main(String[] args) {
		//create a proxy web service factory
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		
		//register the web service
		factory.setAddress("http://localhost:8080/hellowebservice");
		
		//
		factory.setServiceClass(HelloWebService.class);
		
		HelloWebService hello = (HelloWebService)factory.create();
		
		System.out.println("invoking service...");
		System.out.println(hello.sayHello("chy "));
		
	}
}

 

四:补充——项目结构图及jar包


                 

我是实现了后续要记录的内容才回头做的笔记、所以项目图中会有比文章中多的类或接口、如果自己搭建、则只需按照上边一步步来就ok。



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值