使用CXF开发WebService程序的总结(二):创建webservice服务端

1. 配置Apache CXF

  1. 下载 Apache CXF:去官网 http://cxf.apache.org/download.html 下载
  2. 配置环境变量
    使用CXF开发WebService程序的总结(二):创建webservice服务端_第1张图片

2.创建服务端之前先创建一个maven父工程,方便服务端和客户端maven工程的创建和jar包依赖以及插件的集成配置等。

2.1 新建maven工程  ws_parent,其pom配置如下


	4.0.0
	com.lonely.webservice
	ws_parent
	0.0.1-SNAPSHOT
	pom


	
	
		
			
				org.apache.cxf
				cxf-core
				3.2.1
			
			
				org.apache.cxf
				cxf-rt-frontend-jaxws
				3.2.1
			
			
				org.apache.cxf
				cxf-rt-transports-http-jetty
				3.2.1
			
		
	

	
	
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					1.8
					1.8
					UTF-8
				
			
		
	


3.创建服务端

3.1 创建一个maven子工程  ws_server,继承父工程,
在父工程的pom文件中添加如下配置

	ws_server
在子工程中的pom文件添加如下配置

	4.0.0
	
		com.lonely.webservice
		ws_parent
		0.0.1-SNAPSHOT
	
	ws_server

	
	
		
			org.apache.cxf
			cxf-core
		
		
			org.apache.cxf
			cxf-rt-frontend-jaxws
		
		
			org.apache.cxf
			cxf-rt-transports-http-jetty
		
	

3.2 创建一个服务接口
package com.lonely.server;

import javax.jws.WebService;

@WebService
public interface HelloWs {
	public String sayHelloWs(String str);
}
3.3 创建该接口的实现类
package com.lonely.server.impl;

import javax.jws.WebService;

import com.lonely.server.HelloWs;

@WebService
public class HelloWsImpl implements HelloWs {

	@Override
	public String sayHelloWs(String str) {
		return str + ":正在学习web Service~~~~~~~~~~~~~~";
	}

}
3.4 创建一个服务发布类
package com.lonely.server.impl;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.lonely.server.HelloWs;

public class ReleaseClient {

	public static void main(String[] args) {
		System.out.println("WS 服务端 start~~~~~~");
		String address = "http://localhost:8090/sayhello";
		HelloWs helloWs = new HelloWsImpl();
		JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
		// 设置地址
		jaxWsServerFactoryBean.setAddress(address);
		// 设置接口
		jaxWsServerFactoryBean.setServiceClass(HelloWs.class);
		// 设置实现类
		jaxWsServerFactoryBean.setServiceBean(helloWs);
		jaxWsServerFactoryBean.create();
		System.out.println("WS 服务端 started~~~~~~~");
	}
}
3.5 至此,我们启动该类,输入发布地址然后加上 ?wsdl 查看效果
http://localhost:8090/sayhello?wsdl







你可能感兴趣的:(webservice,webservice)