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


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开发服务器端

 

整个项目的目录结构(普通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();
	}

	
}


测试客户端:

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开发WebService客户端

 

执行如下命令(找到webService的客户端的工作空间执行)





刷新客户端项目,可看到如下生成的代码:




编写客户端调用代码:


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,服务器端

1)开发一个Web Service业务接口。该接口要用@WebService修饰

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

3)发布Web Service

2,客户端

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

WSDL-Web Service Definition Language

任何语言实现了Web Service,都需要提供,并暴露WSDL文档

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

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

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



源码下载

 

 

 



  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
好的,下面是一个基于Apache CXFweb service的例子: 1. 首先,创建一个Maven项目,并添加以下依赖: ```xml <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.7</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.3.7</version> </dependency> </dependencies> ``` 2. 创建一个接口,并定义web service的方法: ```java @WebService public interface GreetingService { @WebMethod String sayHello(String name); } ``` 3. 创建一个实现类,并实现接口中定义的方法: ```java @Service @WebService(endpointInterface = "com.example.demo.GreetingService") public class GreetingServiceImpl implements GreetingService { @Override public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 4. 在配置文件中配置CXF: ```yaml cxf: servlet: load-on-startup: 1 url-pattern: /services/* jaxws: properties: javax.xml.ws.wsdl.service: GreetingService javax.xml.ws.wsdl.port: GreetingServicePort ``` 5. 部署应用程序并启动服务器,访问以下URL即可访问web service: ``` http://localhost:8080/services/GreetingService?wsdl ``` 6. 测试web service: ```java public class Client { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(GreetingService.class); factory.setAddress("http://localhost:8080/services/GreetingService"); GreetingService client = (GreetingService) factory.create(); String result = client.sayHello("World"); System.out.println(result); } } ``` 以上就是一个简单的基于Apache CXFweb service例子。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值