首页 > 作文

Java WebService开源框架CXF详解

更新时间:2023-04-04 02:33:22 阅读: 评论:0

cxf简介

cxf是一个开源的webrvice框架。apache cxf = celtix + xfire,开始叫 apache celtixfire,后来更名为 apache cxf 了,以下简称为 cxf。cxf 继承了 celtix 和 xfire 两大开源项目的精华,提供了对 jax-ws 全面的支持,并且提供了多种 binding 、databinding、transport 以及各种 format 的支持,并且可以根据实际项目的需要,采用代码优先(code first)或者 wsdl 优先(ws最大功率dl first)来轻松地实现 web rvices 的发布和使用。

支持多种标准

支持 jax-ws、 jax-wsa、jsr-181 和 saaj;支持 soap 1.1、1.2、ws-i basicprofile、ws-curity、ws-addressing、ws-rm 和 ws-policy;支持 wsdl 1.1 、2.0;支持 mtom;

它支持多种协议,比如:soap1.1,1,2、xml/http、restful http 或者 corba。corba(common object request broker architecture公共对象请求代理体系结构,早期语言使用的ws。c,c++,c#)

cxf是基于soa总线结构,依靠spring完成模块的集成,实现soa方式。

灵活的部署:可以运行有tomcat,jboss,jetty(内置),weblogic上面。

cxf入门案例

我们还以昨天的天气服务为案例来看一下cxf的开发过程。

服务端的实现

1.创建一个空的java项目,创建一个lib目录,将所有jar包放入lib目录  然后为工程引入jar包,选择build path,然后add jars,只用选择cxf-manifest.jar即可。

2.创建一个i接口,需要在接口上添加@webrvice注解@webrvicepublic interface weatherinterface {    public string queryweather(string cityname);}

3.创建i接口实现类 public class weatherinterfaceimpl implements weatherinterface {    public string queryweather(string cityname) {        if("河南".equals(cityname)) {            return "热爆炸";        }el {            return "冰雹";        }    }}

4.发布服务 public class weatherrver {    public static void main(string[] args) {        //创建服务工厂bean        jaxwsrverfactorybean jaxwsrverfactorybean=new jaxwsrverfactorybean();        //设置服务接口        jaxwsrverfactorybean.trviceclass(weatherinterface.class);        //设置服务实现类    麻山药的做法    jaxwsrverfactorybean.trvicebean(new weatherinterfaceimpl());        //设置服务地址        jaxwsrverfactorybean.taddress("http://127.0.0.1:12345/weather");        //创建服务        jaxwsrverfactorybean.create();    }}

5.访问服务的wsdl文件地址,看服务是否发布成功    http://127.0.0.1:12345/weather?wsdl

发布soap1.2的服务端

soap分为1.1版本和1.2版本。jdk1.6并不支持1.2,我们可以通过cxf来发布soap1.2的服务端。
只需要在接口上添加注解 @bindingtype(soapbinding.soap12http_binding)。然后重新发布服务即可

import javax.jws.webrvice;import javax.xml.ws.bindingtype;import javax.xml.ws.soap.soapbinding;@webrvice@bindingtype(soapbinding.soap12http_binding)public interface weatherinterface {    public string queryweather(string cityname);}

客户端的实现

wsdl2java命令是cxf提供的生成客户端的工具,他和wsimport类似,可以根据wsdl生成客户端代码 wsdl2java常用参数:-d,指定输出目录-p,指定包名,如果不指定该参数,默认包名是wsdl的命名空间的倒序 wsdl2java支持soap1.1和soap1.2
1.我们先创建一个客户端项目,然后引入jar包,和上面一样,使用add jars选择cxf-manifest.jar即可  然后使用工具生成客户端  wsdl2java -p com.cad.cxf -d . http://127.0.0.1:12345/weather?wsdl 

2.创建客户端 public class weatherclient {    public static void main(string[] args) {        jaxwsproxyfactorybean jaxwsproxyfactorybean=new jaxwsproxyfactorybean();        //设置服务接口        jaxwsproxyfactorybean.trviceclass(weatherinterface.class);         //设置服务地址        jaxwsproxyfactorybean.taddress("http://127.0.0.1:12345/weather");        //获取服务接口实例        weatherinterface weatherinterface=(weatherinterface) jaxwsproxyfactorybean.create();        //调用方法        string message=weatherinterface.queryweather("河南");        system.out.println(message);    }}

cxf+spring整合发布soap模式的服务

服务端的实现

1.创建web项目,导入jar包 
2.创建i接口 @webrvice@bindingtype(soapbinding.soap12http_binding)public interface weatherinterface {    public string queryweather(string cityname);}
3.创建i实现类 public class weatherinterfaceimpl implements weatherinterface {    public string queryweather(string cityname) {        if("河南".equals(cityname)) {            return "热爆炸";        }el {            return "冰雹";        }    }}
4.配置spring配置文件,applicationcontext.xml  <?xml version="1.0" encoding="utf-8"?><beans xmlns="/d/file/titlepic/"    xmlns:xsi="/d/file/titlepic/xmlschema-instance" xmlns:jaxws="/d/file/titlepic/jaxws.xsd"    xmlns:jaxrs="/d/file/titlepic/jaxrs.xsd" xmlns:cxf="/d/file/titlepic/core.xsd"    xsi:schemalocation="http://www.springframework.org/schema/beans                             /d/file/titlepic/spring-beans.xsd                            http://cxf.apache.org/jaxrs /d/file/titlepic/jaxrs.xsd                            http://cxf.apache.org/jaxws /d/file/titlepic/jaxws.xsd                            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"><!--jaxws:rver发布soap协议的服务 ,对jaxwsrverfactorybean类封装--><!--rviceclass属性是服务接口,address代表地址,因为我们是web服务,不需要输入ip。rvicebean是服务实现类--><jaxws:rver rviceclass="com.cad.cxf.weatherinterface" address="/weather">    <jaxws:rvicebean>        <ref bean="weatherinterfaceimpl"/>    </jaxws:rvicebean></jaxws:rver><bean name="weatherinterfaceimpl" class="com.cad.cxf.weatherinterfaceimpl"></bean></beans>                            
5.配置web.xml <?xml version="1.0" encoding="utf-8"?><web-app xmlns:xsi="/d/file/titlepic/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_2_5.xsd" id="webapp_id" version="2.5">  <display-name>cxfspringdemo</display-name>  //配置tomcat启动时加载spring配置文件   <context-param>    <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提供的rvlet   <rvlet>    <rvlet-name>cxf</rvlet-name>    <rvlet-class>org.apache.cxf.transport.rvlet.cxfrvlet</rvlet-class>  </rvlet>  <rvlet-mapping>    <rvlet-name>cxf</rvlet-name>    <url-pattern>/ws/*</url-pattern>  </rvlet-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>
6.部署到tomcat下,发布服务,并访问 http://localhost:8080/cxfspringdemo/ws/weather?wsdl

客户端的实现

1.创建项目,导入jar包,生成客户端 wsdl2java -p com.cad.cxf -d . http://localhost:8080/cxfspringdemo/ws/weather?wsdl

2.配置spring文件  <?xml version="1.0" encoding="utf-8"?><beans xmlns="/d/file/titlepic/"    xmlns:xsi="/d/file/titlepic/xmlschema-instance" xmlns:jaxws="/d/file/titlepic/jaxws.xsd"    xmlns:jaxrs="/d/file/titlepic/jaxrs.xsd" xmlns:cxf="/d/file/titlepic/core.xsd"    xsi:schemalocation="http://www.springframework.org/schema/beans                             /d/file/titlepic/spring-beans.xsd                            http://cxf.apache.org/jaxrs /d/file/titlepic/jaxrs.xsd                            http://cxf.apache.org/jaxws /d/file/titlepic/jaxws.xsd                            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">    <!-- <jaxws:client实现客户端 ,对jaxwsproxyfactorybean类封装-->       <!-- address是服务地址,rvicelass是服务接口,返回服务实现类-->       <jaxws:client id="weatherclient" address="http://localhost:8080/cxfspringdemo/ws/weather" rviceclass="com.cad.cxf.weatherinterface"/></beans>
3.通过spring容器获取服务实现类,调用方法 public class weatherclient {    public static void main(string[] args) {        applicationcontext context = new classpathxmlapplicationcontext("classpath:applicationcontext.xml");        weatherinterface  weatherinterface = (weatherinterface) context.getbean("weatherclient");        string message=weatherinterface.queryweather("河南");        system.out.println(message);    }}

cxf发布rest模式的服务

rest即表述性状态传递(英文:reprentational state transfer,简称rest),是一种软件架构风格。

因为rest模式的web服务与复杂的soap和xml-rpc对比来讲明显的更加简洁,越来越多的web服务开始采用rest风格设计和实现rest服务采用http 做传输协议,rest 对于http 的利用实现精确的资源定位。

例如:非rest方式:http://ip:port/queryur.action?urtype=student&id=001 rest方式:http://ip:port/ur/student/query/001 
1.创建一个项目,导入cxf jar包 
2.创建一个实体类 student   @xmlrootelement(name="student")可以实现xml和对象之间的转换,name属性指定根元素@xmlrootelement(name="student")public class student {    private int id;    private string name;    private date birthday;    public int getid() {        return id;    }    public void tid(int id) {        this.id = id;    }    public string getname() {        retu优秀党员故事rn name;    }    public void tname(string name) {        this.name = name;    }    public date getbirthday() {        return birthday;    }    public void tbirthday(date birthday) {        this.birthday = birthday;    }}
3.创建i接口 @webrvice//@path("/student")就是指定访问该接口的路径@path("/student")public interface studentinterface {        //指定请求方式,如果服务端发布的时候指定的是get(post),那么客户端访问时必须使用get(post        @get        //指定服务数据类型,可以是xml,json等数据类型        @produces(mediatype.application_xml)        //@path("/query/{id}")指定该方法的路径,“{id}”指参数,多个参数,以“/”隔开,放到“{}”中        @path("/query/{id}")        public student querystudent(@pathparam("id")int id);        @get        @produces(mediatype.application_xml)        @path("/querylist/{name}")        public list<student> querylist(@pathparam("name")string name);}
4.创建i实现类 public class studentinterfaceimpl implements studentinterface {    @override    public student querystudent(int id) {        student s=new student();        s.tid(666);        s.tname("张三");        s.tbirthday(new date());        return s;    }    @override    public list<student> querylist(string name) {        student s=new student();        s.tid(666);        s.tname("张三");        s.tbirthday(new date());        student s2=new student();        s2.tid(888);        s2.tname("李四");        s2.tbirthday(new date());        list<student> l=new arraylist<student>();        l.add(s);        l.add(s2);        return l;    }}
5.发布服务 public class studentrver {    public static void main(string[] args) {        jaxrsrverfactorybean jaxrsrverfactorybean=new jaxrsrverfactorybean();        //设置服务实现类怎么查中考成绩        jaxrsrverfactorybean.trvicebean(new studentinterfaceimpl());        //设置资源类,如果有多个资源类,可以以“,”隔开,例如student.class studentinterface.class都是资源类,但是studentinterfaceimpl里面已经包含了student.class studentinterface.class,所以不用重复指定        jaxrsrverfactorybean.tresourceclass(studentinterfaceimpl.class);         //设置服务地址        jaxrsrverfactorybean.taddress("http://127.0.0.1:12345/class");        //发布服务        jaxrsrverfactorybean.create();    }}
6.测试服务 访问query方法     http://127.0.0.1:12345/class/student/query/001

访问querylist方法     http://127.0.0.1:12345/class/student/querylist/xxx

如果服务端发布时指定请求方式是get(post),客户端必须使用get(post)访问服务端,否则会报异常。
如果在同一方法上同时指定xml和json媒体类型,在get请求下,默认返回xml,在post请求下,默认返回json

cxf+spring整合发布rest模式的服务

综合案例:手机归属地查询

到此这篇关于java webrvice开源框架cxf详解的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。

本文发布于:2023-04-04 02:31:44,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/cc6279824799d98b9324ca3e93efc62f.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:Java WebService开源框架CXF详解.doc

本文 PDF 下载地址:Java WebService开源框架CXF详解.pdf

相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图