WebService与CXF

摘要:
1: Webservice1:WebService做什么?我们将调用此WebService的应用程序称为客户端,将提供此WebService应用程序称之为服务器。当然,除了SAOP,还有其他Web服务技术。XML+XSD、SOAP和WSDL是构成Web服务平台的三大技术。WSDL是一种基于XML的语言,用于描述Web服务及其函数、参数和返回值。在客户端调用WebService服务之前,它需要知道服务的WSDL文件的地址。

一:Webservice 

1:WebService是干什么的?有什么用?

一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用规范

比如,amazon,天气预报系统,淘宝网,校内网,百度等把自己的系统服务以webservice服务的形式暴露出来,让第三方网站和程序可以调用这些服务功能,这样扩展了自己系统的市场占有率

从表面上看,WebService就是一个应用程序向外界暴露出一个能通过Web进行调用的API,也就是说能用编程的方法通过Web来调用这个应用程序。我们把调用这个WebService的应用程序叫做客户端,而把提供这个WebService的应用程序叫做服务端。我们要做的就是开发Webservice接口,调用WebService接口

从深层次看,WebService是建立可互操作的分布式应用程序的新平台,是一个平台,是一套标准。它定义了应用程序如何在Web上实现互操作性,你可以用任何你喜欢的语言,在任何你喜欢的平台上写Web service ,只要我们可以通过Web service标准对这些服务进行查询和访问。

2:什么是SAOP??什么是WSDL??

2.1 SAOP:

SAOP是一种WebService平台技术

SOAP协议 = HTTP协议 + XML数据格式

WebService通过HTTP协议发送请求和接收结果时,发送的请求内容和结果内容都采用XML格式封装,并增加了一些特定的HTTP消息头,以说明HTTP消息的内容格式,这些特定的HTTP消息头和XML内容格式就是SOAP协议。

当然除了SAOP还有其他WebService技术,XML+XSD,SOAP和WSDL就是构成WebService平台的三大技术。

2.2  WSDL(Web Service Description Language)

  是一个用来描述Web服务的说明如何与Web服务通信的xml语言,为用户提供详细的接口说明书

好比我们去商店买东西,首先要知道商店里有什么东西可买,然后再来购买,商家的做法就是张贴广告海报。 WebService也一样,WebService客户端要调用一个WebService服务,首先要有知道这个服务的地址在哪,以及这个服务里有什么方法可以调用,所以,WebService务器端首先要通过一个WSDL文件来说明自己家里有啥服务可以对外调用,服务是什么(服务中有哪些方法,方法接受的参数是什么,返回值是什么),服务的网络地址用哪个url地址表示,服务通过什么方式来调用。

 WSDL(Web Services Description Language)就是这样一个基于XML的语言,用于描述Web Service及其函数、参数和返回值。

一些最新的开发工具既能根据你的Web service生成WSDL文档,又能导入WSDL文档,生成调用相应WebService的代理类代码。

WSDL文件保存在Web服务器上,通过一个url地址就可以访问到它。客户端要调用一个WebService服务之前,要知道该服务的WSDL文件的地址。WebService服务提供商可以通过两种方式来暴露它的WSDL文件地址:1.注册到UDDI服务器,以便被人查找;2.直接告诉给客户端调用者。

 

3:什么是REST

https://www.cnblogs.com/loveis715/p/4669091.html

REST 是一种软件架构模式,只是一种风格,rest服务采用HTTP 做传输协议,REST 对于HTTP 的利用实现精确的资源定位。

Rest要求对资源定位更加准确,如下:

非rest方式:http://ip:port/queryUser.action?userType=student&id=001

Rest方式:http://ip:port/user/student/query/001

Rest方式表示互联网上的资源更加准确,但是也有缺点,可能目录的层级较多不容易理解。

Rest不再需要生成客户端,直接获取数据

二:CXF 

1:简介

  1.1  CXF是什么?有什么用?优点

  Apache CXF 是一个开源的web Services 框架,CXF 帮助您构建和开发 web Services ,它支持多种协议,支持数据格式:XML,JSON(仅在REST方式下支持)

2:CXF的基础知识

2.1:安装配置

官网下载,解压

环境变量配置

WebService与CXF第1张

WebService与CXF第2张

3:CXF与Spring整合发布SAOP与REST项目

3.1 CXF+Spring整合发布SAOP协议服务

3.1.1服务端

  开发步骤:

  第一步:创建web项目,导入jar包,maven项目添加坐标

  maven需三个:cxf-core, cxf-rt-frontend-jaxws,cxf-rt-transports-http-jetty

  第二步:创建SEI接口(SEI在webservice中称为portType,在java中称为接口)

 1 import javax.jws.WebService;
 2 import javax.xml.ws.BindingType;
 3 import javax.xml.ws.soap.SOAPBinding;
 4 
 5 /**
 6  * 
 7  * <p>Title: WeatherInterface.java</p>
 8  * <p>Description:SEI接口</p>
 9  */
10 @WebService
11 @BindingType(SOAPBinding.SOAP12HTTP_BINDING)
12 public interface WeatherInterface {
13 
14     public String queryWeather(String cityName);
15     
16 }

  第三步:创建SEI实现类

 1 public class WeatherInterfaceImpl implements WeatherInterface {
 2 
 3     @Override
 4     public String queryWeather(String cityName) {
 5         System.out.println("from client..."+cityName);
 6         if("北京".equals(cityName)){
 7             return "冷且霾";
 8         } else {
 9             return "暖且晴";
10         }
11     }
12 
13 }

 第四步:配置Spring配置文件beans.xml

   用<jaxws:server标签发布服务,设置  1.服务地址; 2.设置服务接口; 3设置服务实现类

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
 4     xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6                             http://www.springframework.org/schema/beans/spring-beans.xsd
 7                             http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
 8                             http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
 9                             http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
10     <!-- <jaxws:endpoint发布SOAP协议的服务 ,对Endpoint类封装-->    
11     <jaxws:endpoint address="/hello" implementor="com.xqc.ws.cxf.server.HelloWorld"/>                    
12     
13     <!-- <jaxws:server发布SOAP协议的服务 ,对JaxWsServerFactoryBean类封装-->
14     <jaxws:server address="/weather" serviceClass="com.xqc.ws.cxf.server.WeatherInterface">
15         <jaxws:serviceBean>
16             <ref bean="weatherInterface"/>
17         </jaxws:serviceBean>
18         
19         <!-- 配置拦截器 -->
20         <jaxws:inInterceptors>
21             <ref bean="inIntercepter"/>
22         </jaxws:inInterceptors>
23         <jaxws:outInterceptors>
24             <ref bean="outIntercepter"/>
25         </jaxws:outInterceptors>
26     </jaxws:server>
27     <!-- 配置拦截器的bean -->
28     <bean name="inIntercepter" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
29     <bean name="outIntercepter" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
30     
31     <!-- 配置服务实现类 -->
32     <bean name="weatherInterface" class="com.xqc.ws.cxf.server.WeatherInterfaceImpl"/>
33 </beans>

  第五步:配置Web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 3   <display-name>ws_2_cxf_spring_server</display-name>
 4   
 5   <!-- 设置spring的环境 ,加载spring配置文件 -->
 6   <context-param>
 7       <!--contextConfigLocation是不能修改的  -->
 8       <param-name>contextConfigLocation</param-name>
 9       <param-value>classpath:beans.xml</param-value>
10   </context-param>
11   <listener>
12       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
13   </listener>
14   
15   <!-- 配置CXF的Servlet -->
16   <servlet>
17       <servlet-name>CXF</servlet-name>
18       <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
19   </servlet>
20   <servlet-mapping>
21       <servlet-name>CXF</servlet-name>
22       <url-pattern>/ws/*</url-pattern>
23   </servlet-mapping>
24   
25   <welcome-file-list>
26     <welcome-file>index.html</welcome-file>
27     <welcome-file>index.htm</welcome-file>
28     <welcome-file>index.jsp</welcome-file>
29     <welcome-file>default.html</welcome-file>
30     <welcome-file>default.htm</welcome-file>
31     <welcome-file>default.jsp</welcome-file>
32   </welcome-file-list>
33 </web-app>

  第六步:部署到tomact下,启动tomact

  

  第七步:测试服务

     浏览器输入:WSDL地址规则:http://ip:端口号/项目名称/servlet拦截路径/服务名称?wsdl

  第八步:Endpoint标签发布服务

<jaxws:endpoint>标签

     添加文件 

1 @WebService
2 public class HelloWorld {
3     public String sayHello(String name){
4         return "hello,"+name;
5     }
6     
7 }

    在beans中添加配置

1      <!-- <jaxws:endpoint发布SOAP协议的服务 ,对Endpoint类封装-->    
2      <jaxws:endpoint address="/hello" implementor="com.xqc.ws.cxf.server.HelloWorld"/>      

    访问:http://ip:端口号/项目名称/servlet拦截路径/      例如:http://localhost:8080/ws_2_cxf_spring_server/ws/

 3.1.2 客户端(近写一个javase的客户端演示一下,客户端可以很多)

  开发步骤:

第一步:引入jar包

第二步:生成客户端代码

第三步:配置spring配置文件,applicationContent.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
                            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
                            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
    <!-- <jaxws:client实现客户端 ,对JaxWsProxyFactoryBean类封装-->    
    <jaxws:client id="weatherClient" address="http://127.0.0.1:8080/ws_2_cxf_spring_server/ws/weather" serviceClass="com.xqc.cxf.weather.WeatherInterface"/>
</beans>

第四步:从spring上下文件获取服务实现类

第五步:调用查询方法,打印

 1 package com.xqc.cxf.client;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import com.xqc.cxf.weather.WeatherInterface;
 7 
 8 public class WeatherClient {
 9 
10     public static void main(String[] args) {
11         //初始化spring的上下文
12         ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
13         WeatherInterface  weatherInterface = (WeatherInterface) context.getBean("weatherClient");
14         String weather = weatherInterface.queryWeather("保定");
15         System.out.println(weather);
16     }
17 }

3.2CXF+Spring整合发布REST服务

3.2.1服务端

开发步骤:

第一步:导入jar包

第二步:创建学生pojo类,要加入@ XmlRootElement

 1 package com.xqc.ws.rest.pojo;
 2 
 3 import java.util.Date;
 4 
 5 import javax.xml.bind.annotation.XmlRootElement;
 6 
 7 /**
 8  * 
 9  * <p>Title: Student.java</p>
10  * <p>Description:学生实体类</p>
11  */
12 @XmlRootElement(name="student")//@XmlRootElement可以实现对象和XML数据之间的转换
13 public class Student {
14 
15     private long id;
16     
17     private String name;
18     
19     private Date birthday;
20 
21     public long getId() {
22         return id;
23     }
24 
25     public void setId(long id) {
26         this.id = id;
27     }
28 
29     public String getName() {
30         return name;
31     }
32 
33     public void setName(String name) {
34         this.name = name;
35     }
36 
37     public Date getBirthday() {
38         return birthday;
39     }
40 
41     public void setBirthday(Date birthday) {
42         this.birthday = birthday;
43     }
44     
45 }

第三步:创建SEI接口

 1 package com.xqc.ws.rest.server;
 2 
 3 import java.util.List;
 4 import javax.jws.WebService;
 5 import javax.ws.rs.GET;
 6 import javax.ws.rs.Path;
 7 import javax.ws.rs.PathParam;
 8 import javax.ws.rs.Produces;
 9 import javax.ws.rs.core.MediaType;
10 
11 import com.xqc.ws.rest.pojo.Student;
12 
13 /**
14  * 
15  * <p>Title: StudentInterface.java</p>
16  * <p>Description:学生接口</p>
17  */
18 @WebService
19 @Path("/student")//@Path("/student")就是将请求路径中的“/student”映射到接口上
20 public interface StudentInterface {
21 
22     //查询单个学生
23     @GET//指定请求方式,如果服务端发布的时候指定的是GET(POST),那么客户端访问时必须使用GET(POST)
24     @Produces(MediaType.APPLICATION_XML)//指定服务数据类型
25     @Path("/query/{id}")//@Path("/query/{id}")就是将“/query”映射到方法上,“{id}”映射到参数上,多个参数,以“/”隔开,放到“{}”中
26     public Student query(@PathParam("id")long id);
27     
28     //查询多个学生
29     @GET//指定请求方式,如果服务端发布的时候指定的是GET(POST),那么客户端访问时必须使用GET(POST)
30     @Produces("application/json;charset=utf-8")//指定服务数据类型
31     @Path("/queryList/{name}")//@Path("/queryList/{name}")就是将“/queryList”映射到方法上,“{name}”映射到参数上,多个参数,以“/”隔开,放到“{}”中
32     public List<Student> queryList(@PathParam("name")String name);
33     
34 }

第四步:创建SEI实现类

 1 package com.xqc.ws.rest.server;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Date;
 5 import java.util.List;
 6 
 7 import com.xqc.ws.rest.pojo.Student;
 8 
 9 /**
10  * 
11  * <p>Title: StudentInterfaceImpl.java</p>
12  * <p>Description:学生的实现类</p>
13  */
14 public class StudentInterfaceImpl implements StudentInterface {
15 
16     @Override
17     public Student query(long id) {
18         Student st = new Student();
19         st.setId(110);
20         st.setName("张三");
21         st.setBirthday(new Date());
22         return st;
23     }
24 
25     @Override
26     public List<Student> queryList(String name) {
27         
28         Student st = new Student();
29         st.setId(110);
30         st.setName("张三");
31         st.setBirthday(new Date());
32         
33         Student st2 = new Student();
34         st2.setId(120);
35         st2.setName("李四");
36         st2.setBirthday(new Date());
37         
38         List<Student> list = new ArrayList<Student>();
39         list.add(st);
40         list.add(st2);
41         return list;
42     }
43 
44 }

第五步:

配置Spring配置文件,beans.xml,<jaxrs:server,设置1.服务地址;2.服务实现类

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
 4     xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6                             http://www.springframework.org/schema/beans/spring-beans.xsd
 7                             http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
 8                             http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
 9                             http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
10     <!-- <jaxrs:server发布REST的服务 ,对JAXRSServerFactoryBean类封装-->    
11     <jaxrs:server address="/user">
12         <jaxrs:serviceBeans>
13             <ref bean="studentInterface"/>
14         </jaxrs:serviceBeans>
15     </jaxrs:server>
16     
17     <!-- 配置服务实现类 -->
18     <bean name="studentInterface" class="com.xqc.ws.rest.server.StudentInterfaceImpl"/>
19 </beans>

第六步:配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ws_2_cxf_spring_server</display-name>
  
  <!-- 设置spring的环境 -->
  <context-param>
      <!--contextConfigLocation是不能修改的  -->
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 配置CXF的Servlet -->
  <servlet>
      <servlet-name>CXF</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>CXF</servlet-name>
      <url-pattern>/ws/*</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

第七步:部署到tomcat下,启动tomcat

第八步:测试服务

         REST服务的使用说明书地址:

http://127.0.0.1:8080/ws_4_cxf_rest_spring_server/ws/user?_wadl

http://127.0.0.1:8080/ws_4_cxf_rest_spring_server/ws/user/student/query/110 查询单个学生,返回XML数据

1 <student>
2 <birthday>2015-11-27T15:22:14.240+08:00</birthday>
3 <id>110</id>
4 <name>张三</name>
5 </student>

http://127.0.0.1:8080/ws_4_cxf_rest_spring_server/ws/user//student/queryList/110?_type=json 查询多个学生,返回JSON

{"student":[{"birthday":"2015-11-27T15:24:21.565+08:00","id":110,"name":"张三"},{"birthday":"2015-11-27T15:24:21.565+08:00","id":120,"name":"李四"}]}

3.2.2:客户端:因为返回的直接就是数据,其实直接解析就可以,DOM4J解析

直接用新建html然后使用Ajax使用即可

———————————————————————————————————————————————

综合案例训练:

———————————————————————————————————————————————

免责声明:文章转载自《WebService与CXF》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ASP.NET Core 的Windows和IIS宿主(自动翻译记录)launcher- 第三方应用图标替换下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Spring batch学习 (1)

          Spring Batch 批处理框架 埃森哲和Spring Source研发                          主要解决批处理数据的问题,包含并行处理,事务处理机制等。具有健壮性 可扩展,和自带的监控功能,并且支持断点和重发。让程序员更加注重于业务实现。           Spring Batch 结构如下      ...

Tornado 【简述】

前言 python 旗下,群英荟萃,豪杰并起。单是用于 web 开发的,就有 webpy、web2py、bottle、pyramid、zope2、flask、tornado、django 等等,不一而足。最近几年较为流行的,大概也就是flask、tornado 和 django 了。 关于以上各个 web 开发框架的性能比较,上网一搜,铺天盖地——这不是...

Java Web基础 --- Servlet 综述(理论篇)

摘要:   Web 技术成为当今主流的互联网 Web 应用技术之一,而 Servlet 是 Java Web 技术的核心基础。本文首先从请求/响应架构应用的大背景谈起 Servlet 的由来,明确 Servlet 的产生动机,并揭示了 Servlet 的本质以及其在标准MVC模式中所扮演的角色。紧接着,给出了 Servlet族的继承结构,并对族内的接口和抽...

Spring定时任务的几种实现

近日项目开发中需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息,借此机会整理了一下定时任务的几种实现方式,由于项目采用spring框架,所以我都将结合 spring框架来介绍。 一.分类 从实现的技术上来分类,目前主要有三种技术(或者说有三种产品): Java自带的java.util.Timer类,这个类允许你调度一个java....

使用 Spring Security 保护 Web 应用的安全

安全一直是 Web 应用开发中非常重要的一个方面。从安全的角度来说,需要考虑用户认证和授权两个方面。为 Web 应用增加安全方面的能力并非一件简单的事情,需要考虑不同的认证和授权机制。Spring Security 为使用 Spring 框架的 Web 应用提供了良好的支持。本文将详细介绍如何使用 Spring Security 框架为 Web 应用提供安...

认识Windows Communication Foundation

            好久没有写博客,作为一个程序员,我居然习惯了不写博客。也难怪这些年来,一直原地踏步,无所长进。真是一份付出一份收获,种瓜得瓜,种豆得豆。 Windows Communication Foundation   是一种非常实用、非常强大的技术。属于.NET范畴内的,微软推出的技术。在这个.NET即将失去市场的日子里,也只有WCF这个技术...