码迷,mamicode.com
首页 > Web开发 > 详细

Web Service学习-CXF开发Web Service实例demo(一)

时间:2017-06-24 21:47:23      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:return   实现   工作   配置   download   lan   imp   apache   不同的   


Web Service是什么?

 

 Web Service不是框架。更甚至不是一种技术。

而是一种跨平台,跨语言的规范

 

Web Service解决什么问题:

 

    为了解决不同平台,不同语言所编写的应用之间怎样调用问题。比如。有一个C语言写的程序。它想去调用java语言写的某个方法。

 

集中解决:1,远程调用 2。跨平台调用 3,跨语言调用

 

实际应用:

 

1。同一个公司的新,旧系统的整合。Linux上的java应用,去调用windows平台的C应用

2,不同公司的业务整合。业务整合就带来不同公司的系统整合。不同公司的系统可能存在平台不同,语言不同的问题

3,内容聚合。一个应用,比方须要提供,天气预报,股票行情,黄金行情等。

 

CXF与Web Service的关系

 

    CXF是apache旗下的开源框架,由Celtix+ XFire这两门经典的框架合成。是一套很流行的web service框架。

 

CXF方式实现Web Service服务demo

 

1,配置环境变量


在CLASSPATH中加入E:\apache-cxf-2.6.2\lib

新建CXF_HOMEE:\apache-cxf-2.6.2

在PATH中加入 %CXF_HOME%\bin


技术分享


2。使用CXF开发Web Service开发server端

 

整个项目的文件夹结构(普通java项目):


技术分享


接口:

package com.tgb.service;

import javax.jws.WebService;


@WebService
public interface HelloWorld{
	
	public String sayHi(String name);
	
}

实现类:

package com.tgb.service.impl;

import java.util.Date;

import javax.jws.WebService;

import com.tgb.service.HelloWorld;

@WebService(endpointInterface="com.tgb.service.HelloWorld",serviceName="HelloWorldImpl")
public class HelloWorldImpl implements HelloWorld{

	public String sayHi(String name) {
			
		return name+"您好!

如今时间为:"+new Date(); } }


測试client:

package com.tgb.client;

import javax.xml.ws.Endpoint;

import com.tgb.service.HelloWorld;
import com.tgb.service.impl.HelloWorldImpl;

public class ServerMain {

	public static void main(String[] args){
		
		HelloWorld hw=new HelloWorldImpl();
		//调用endpoint的publish方法,来公布web service
		Endpoint.publish("http://192.168.24.215/hjy",hw);
		System.out.println("Web Service暴露成功");
	}
}

启动程序:


技术分享


查看wsdl


技术分享


3,使用CXF开发WebServiceclient

 

运行例如以下命令(找到webService的client的工作空间运行)


技术分享



刷新client项目,可看到例如以下生成的代码:


技术分享


编写client调用代码:


package hjy;

import com.tgb.service.HelloWorld;
import com.tgb.service.impl.HelloWorldImpl;

public class ClientMain {

	public static void main(String[] args){
		HelloWorldImpl factory=new HelloWorldImpl();
		//此处返回的仅仅是远程Web Service的代理
		HelloWorld hw=factory.getHelloWorldImplPort();
		System.out.println(hw.sayHi("hejingyuan"));
	}
}

运行结果:

 

hejingyuan您好!如今时间为:TueJul 28 14:09:07 CST 2015

 

 

总结:

 

使用CXF开发Web Service共同拥有例如以下几个步骤:

 

1,server端

1)开发一个Web Service业务接口。

该接口要用@WebService修饰

2)开发一个Web Service业务实现类。该实现类也须要用@WebService修饰

3)公布Web Service

2,client

1)调用CXF提供的wsdl2java工具,依据WSDL文档生成对应的java代码。

WSDL-Web Service Definition Language

不论什么语言实现了Web Service,都须要提供,并暴露WSDL文档

2)找到wsdl2java所生成类中,一个继承了Service的类

该类的实例可当成工厂来使用

3)调用Service子类的实例的getXxxPort方法。返回远程Web Service的代理



源代码下载

 

 

 



Web Service学习-CXF开发Web Service实例demo(一)

标签:return   实现   工作   配置   download   lan   imp   apache   不同的   

原文地址:http://www.cnblogs.com/yfceshi/p/7074468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!