WebService learning summary (12) - cxf use case (calling weather)

1. What is CXF?

     Apache CXF = Celtix + Xfire, originally called Apache CeltiXfire, and later renamed Apache CXF, hereinafter referred to as CXF. Apache CXF is an open source web services framework, CXF helps you build and develop web services, it supports a variety of protocols, such as: SOAP1.1,1,2  XML / HTTP , REST ful or CORBA .

     REST ful: A style not a protocol. Its idea is that everything on the network is abstracted into resources, and each resource corresponds to a unique resource identifier.

     Cxf is based on the SOA bus structure, and relies on spring to complete the integration of modules and implement the SOA method.

     Flexible deployment: can run on Tomcat, Jboss, Jetty (built-in), weblogic.

 

Configuration of environment variables

CXF_HOME: Installation directory. 

Path:  bin directory.

 

Second, use CXF to publish the WebService of the jax-ws specification

1. Server

Step 1: Create a java project

Step 2: Import the jar package of cxf. 138

Step 3: Write the SEI interface. The @Webservice annotation needs to be added to the SEI interface.

Step 4: Write the SEI implementation class. No annotation is required.

Step 5: Publish the service

          1. Create a JaxWsServerFactoryBean object

          2. Set the SEI interface setServiceClass

          3. Set the SEI implementation class object. setServiceBean

          4、设置服务发布地址。setAddress

          5、发布服务。Create

POJO

image

 

SEI

image

 

SEI实现类

image

 

发布服务

image

 

查看服务

image

 

2.wsdl3java命令

image

image

 

3.客户端开发

(1)传统客户端开发

image

结果:

日期: 2016-07-27 
最高温度: 30 
最低温度: 20 
天气信息: 北京: 今天很热 
============================================ 
日期: 2016-07-28 
最高温度: 31 
最低温度: 21 
天气信息: 北京: 今天小雨 
============================================ 
日期: 2016-07-29 
最高温度: 32 
最低温度: 22 
天气信息: 北京: 今天大雨 
============================================

 

(2)使用CXF实现客户端

步骤:

1、导入cxf的jar包

2、创建一个JaxWsProxyFactoryBean对象

3、设置一个PortType的class。

4、设置一个address。服务端的url包括ip及端口。

5、调用create方法生成PortType代理对象。

6、调用服务端方法

7、打印结果。

image

结果:

日期: 2016-07-27 
最高温度: 30 
最低温度: 20 
天气信息: 上海: 今天很热 
============================================ 
日期: 2016-07-28 
最高温度: 31 
最低温度: 21 
天气信息: 上海: 今天小雨 
============================================ 
日期: 2016-07-29 
最高温度: 32 
最低温度: 22 
天气信息: 上海: 今天大雨 
============================================

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324671804&siteId=291194637