CXF

概念

 什么CXF它是一个开源的webservice框架提供大量功能特性实现快速开发

CXF支持的协议:SOAP1.1/1.2REST

 CXF支持的数据格式:XMLJSON(只在REST下支持

与EndPoint不同,CXF支持接口形式的发布,Endpoint不支持接口发布

配置

目录

20013005_gcbN.jpg 

配置

JAVA_HOMEjdk安装目录bin的上一级

CXF_HOMEcxf的目录,bin的上一级

Path = ;%JAVA_HOME%\bin;%CXF_HOME%\bin;

CLASSPATH=.;%CXF_HOME%\lib\cxf-manifest.jar


直接访问服务地址会报如下异常,原因CXF发布服务未提供说明界面需直接访问wsdl地址

20013007_r44g.jpg



利用CXF服务端开发

开发步骤:

第一步:创建一个java工程

第二步:导入cxfjar包。138

第三步:编写SEI接口。需要在SEI接口上添加@Webservice注解。

第四步:编写SEI实现类。不需要加注解。

第五步:发布服务

1、创建一个JaxWsServerFactoryBean对象

2、设置SEI接口setServiceClass

3、设置SEI实现类对象。setServiceBean

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

5、发布服务。Create

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class WeatherServer {
     public static void main(String[] args) {
         //JaxWsServerFactoryBean发布一个webservice服务
         JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
         //设置服务接口
         jaxWsServerFactoryBean.setServiceClass(WeatherInterface. class );
         //设置服务实现类实例
         jaxWsServerFactoryBean.setServiceBean( new WeatherInterfaceImpl());
         //设置服务地址
         jaxWsServerFactoryBean.setAddress( "http://127.0.0.1:12345/weather" );
         //发布服务
         jaxWsServerFactoryBean.create();
     }
}

利用CXF客户端开发

开发步骤:

引入jar包:

第二步:生成客户端代码

Ø Wsdl2java:它cxf提供客户端生成命令,支持SOAP1.1SOAP1.2其他和wsimport类似

Ø 常用参数:-d,指定输出目录;-p,指定包名如果不指定也是使用命名空间的倒序


第三步:使用生成的客户端代码调用服务端JaxWsProxyFactoryBean,设置两个参数:1.服务接口2.服务地址,create方法创建一个服务接口的实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class WeatherClient {
     public static void main(String[] args) {
         //JaxWsProxyFactoryBean去调用服务端
         JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
         //设置服务接口
         jaxWsProxyFactoryBean.setServiceClass(WeatherInterface. class );
         //设置服务地址,不是WSDL地址
         jaxWsProxyFactoryBean.setAddress( "http://127.0.0.1:12345/weather" );
         //获取接口实现类的实例
         WeatherInterface weatherInterface = (WeatherInterface) jaxWsProxyFactoryBean.create();
         //调用查询方法
         String weather = weatherInterface.queryWeather( "北京" );
         System.out.println(weather);
     }
}

CXFSpring整合发布SOAP协议的服务

<jaxws:endpoint>标签Endpoint发布方式的一个封装,不需要接口的

<jaxws:server>标签JaxWsServerFactoryBean一个封装,可以发布接口

服务端:

第一步创建web的项目

第二步导入jar

第三步发接口和实现类

第四步配置spring的配置文件:applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<? xml version = "1.0" encoding = "UTF-8" ?>
     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:endpoint发布一个无需接口的服务 -->
     < jaxws:endpoint address = "/hello" implementor = "cn.itcast.ws.cxf.server.HelloWorld" >
     </ jaxws:endpoint >
     
     <!===============================================================================-->
     <!-- <jaxws:server发布一个带接口的服务 -->
     < jaxws:server address = "/weather" serviceClass = "cn.itcast.ws.cxf.server.WeatherInterface" >
         < jaxws:serviceBean >
             < ref bean = "weatherInterface" />
         </ jaxws:serviceBean >
     </ jaxws:server >
     <!-- 配置一个服务实现类的bean -->
     < bean name = "weatherInterface" class = "cn.itcast.ws.cxf.server.WeatherInterfaceImpl" />
</ beans >

第五步配置CXFservletspring配置文件,配置到web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<? xml version = "1.0" encoding = "UTF-8" ?>
   < display-name >ws_2_cxf_spring_server</ display-name >
   <!-- spring配置文件 -->
   < context-param >
   <!-- contextConfigLocation名字是固定的,不能更改, 如果不配置,去默认路径/WEB-INF/applicationContext.xml,这个路径也不能更改-->
     < param-name >contextConfigLocation</ param-name >
     <!-- 可以配置称WEB-INF/classes/applicationContext.xml -->
     < param-value >classpath:applicationContext.xml</ param-value >
   </ context-param >
   <!-- 配置spring的listener,加载配置文件,初始化spring上下文-->
   < 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 >
   <!-- 和上边的servletname保持一致 -->
     < servlet-name >CXF</ servlet-name >
     <!--
     路径映射:
     1.以"/"开头且以"/*"结尾,这种方式叫路径映射
     2.以"*."结尾,比如*.do,这叫扩展映射
     3.只写"/",指定默认的servlet,
     4.其他的映射方式,都是精确映射,
     路径拦截规则顺序:
     1.精确映射
     2.路径映射
     3.扩展映射
     4.默认servlet
      -->
     < 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

第七步:测试服务是否发布成功

wsdl地址访问规则:

http://ip:端口号/项目名称/servlet拦截路径/发布的服务?wsdl





转载于:https://my.oschina.net/mickeymouse/blog/519128

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值