日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > 互聯網 > WebService之CXF框架

WebService之CXF框架

來源:程序員人生   發布時間:2016-06-16 17:48:12 閱讀次數:4032次

本文主要包括以下內容

  1. ant工具的使用
  2. 利用cxf實現webservice
  3. cxf與spring整合
  4. ajax訪問webservice

ant 工具

1、為何要用到ant這個工具呢?

Ant做為1種工具已廣泛被使用,并且歷史悠久。
使用ant的內置命令,可以編譯java源文件(javac),運行java文件(java),給class文件打包(jar、war、ear),
也能夠創建(mkdir)、刪除(del)、拷貝(copy),乃至可使用ant履行sql文件。
由于ant是用xml語言寫成的文件,并取默許名為build.xml文件。
所以,今后大家應當在見到名為build.xml文件時知道這是1個ant的文件。

ant 工具后面跟的是任務的名稱

  • ant server 運行了Server類,發布了1個webservice

  • ant client 調用已發布的webservice

  • ant clean 清除已生成的class 文件

  • ant war 將java 項目打成1個war 包

  • ant deploy -Dtomcat=true 把打成的war 拷貝到tomcat 的webapp 下面去。

  • ant undeploy -Dtomcat=true; 卸載tomcat 下面的項目..

CXF發布服務與調用服務

用cxf 框架提供的類發布1個服務

方法1

使用cxf 提供 ServerFactoryBean 來發布webservice
被發布的類當中可以不需要標注webservice 注解,類當中可以不包括有效的方法,
如果沒有包括有效的方法.它會提供1個空的服務.

//創建發布服務的類... ServerFactoryBean bean=new ServerFactoryBean(); bean.setAddress("http://192.168.9.100:8080/server");//服務對外的訪問地址 bean.setServiceClass(CxfWebService.class);//設置服務類的接口類型,如果沒有接口則為當前類.. bean.setServiceBean(new CxfWebService());//設置服務類的實現 bean.create();//發布服務

有接口的情況

package com.zj.server; import org.apache.cxf.interceptor.LoggingInInterceptor; import org.apache.cxf.interceptor.LoggingOutInterceptor; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import cn.itcast.webservice.userService.UserService; import cn.itcast.webservice.userService.UserServiceImpl; /** * * 使用cxf 提供的類 JaxWsServerFactoryBean 來發布1個帶接口的webservice ... * * @ 作者 zhuwu@itcast.cn * */ public class PublishUserService { /** * @param args */ public static void main(String[] args) { //創建發布服務的 類... JaxWsServerFactoryBean bean=new JaxWsServerFactoryBean(); //設置對外的訪問地址 bean.setAddress("http://10.129.69.114:7418/userService"); bean.setServiceClass(UserService.class);//設置接口類型... bean.setServiceBean(new UserServiceImpl());//設置接口的實現類... //我們可以在發布服務的時候添加消息攔截器 //攔截客戶端往服務端 發送的要求的消息 bean.getInInterceptors().add(new LoggingInInterceptor()); //攔截服務端往客戶端返回的消息... bean.getOutInterceptors().add(new LoggingOutInterceptor()); bean.create(); } }

第2種發布方式

使用cxf 框架提供的類 jaxWsServerFactoryBean 發布webService jaxWsServerFactoryBean 是 ServerFactoryBean 的子類... jaxWsServerFactoryBean bean=new jaxWsServerFactoryBean(); bean.setAddress("http://192.168.9.100:8080/server");//服務對外的訪問地址 bean.setServiceClass(CxfWebService.class);//設置服務類的接口類型,如果沒有接口則為當前類.. bean.setServiceBean(new CxfWebService());//設置服務類的實現 bean.create();//發布服務

客戶端:

方法1
用cxf 框架提供的類調用服務.. (需要依賴1個接口,通過wsimport 生成的代碼當中獲得…)

//創建調用webservice 服務的類... ClientProxyFactoryBean bean=new ClientProxyFactoryBean(); bean.setAddress("http://192.168.9.100:8080/server");//設置訪問地址... bean.setServiceClass(CxfWebServicePortType.class);//設置服務的接口... //創建接口類型... CxfWebServicePortType cxfWebServicePortType=(CxfWebServicePortType) bean.create(); cxfWebServicePortType.sayHello();

方法2

使用cxf 提供類 JaxWsProxyFactoryBean 來調用 webservice 的服務端…….

JaxWsProxyFactoryBean 是 ClientProxyFactoryBean 的子類... //創建調用服務的類... JaxWsProxyFactoryBean bean=new JaxWsProxyFactoryBean(); //設置訪問地址 bean.setAddress("http://192.168.9.100:7418/userService"); //設置接口類型... bean.setServiceClass(UserService.class); UserService us=(UserService) bean.create(); String data=us.getUserById(1); System.out.println(data);

調用原則: 總結.

服務端: 客戶端
ServerFactoryBean ————ClientProxyFactoryBean
JaxWsServerFactoryBean—————-JaxWsProxyFactoryBean

JaxWsServerFactoryBean 可以發布soap1.2 版本的協議….發布服務的時候,
我們最好被發布的服務類要面向接口編程..

命令:wsdl2java

  wsdl2java 是cxf 框架給我們提供的命令,這個命令的作用與wsimport 類似...

攔截器:

cxf 框架中提供了攔截器的機制,我們可以通過攔截器獲得到客戶端與服務端進行交互的時候的數據格式
//創建發布服務的 類... JaxWsServerFactoryBean bean=new JaxWsServerFactoryBean(); //設置對外的訪問地址 bean.setAddress("http://192.168.9.100:7418/userService"); bean.setServiceClass(UserService.class);//設置接口類型... bean.setServiceBean(new UserServiceImpl());//設置接口的實現類... //我們可以在發布服務的時候添加消息攔截器 //攔截客戶端往服務端 發送的要求的消息 bean.getInInterceptors().add(new LoggingInInterceptor()); //攔截服務端往客戶端返回的消息... bean.getOutInterceptors().add(new LoggingOutInterceptor()); bean.create();

cxf 與web 項目的整合

由于cxf的web項目已集成了Spring所以,cxf的服務類都是在spring的配置文件中完成的。以下是步驟:

  • 第1步:建立1個web項目。
  • 第2步:準備所有jar包。將cxf_home\lib項目下的所有jar包全部copy到新項目的lib目錄下,里面已包括了spring3.0的jar包。
  • 第3步:在web.xml中配置cxf的核心servlet,CXFServlet。
  • 第4步:創建(最好是Copy)cxf-servlet.xml文件。這是1個spring的配置文件。

服務器實現了

發布1個不帶接口的webservice

<!-- 1,id,2,服務對外的訪問地址,3,提供服務的實現類.. --> <jaxws:endpoint id="helloService" address="/helloService" implementor="cn.itcast.cxf.spring.HelloService"></jaxws:endpoint>

發布1個帶接口的webservice

web.xml配置以下

<?xml version="1.0" encoding="UTF⑻"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 通過服務器啟動,解析spring 的配置,可以解決第1次訪問 org.apache.cxf.transport.servlet.CXFServlet 去解析spring配置,致使第1次訪問webservice 慢的問題... --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/cxf-servlet.xml</param-value> </context-param> <servlet> <!-- 配置cxf --> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <init-param> <!-- 配置Spring的配置文件 --> <param-name>config-location</param-name> <!-- 通過servlet 去解析此配置文件,會致使第1次訪問很慢, 這是1個spring 的配置文件, --> <param-value>/WEB-INF/cxf-servlet.xml</param-value> <!-- cxf 的啟動原理,依托servlet 首先我們在閱讀器上面敲地址欄,進入到 org.apache.cxf.transport.servlet.CXFServlet 履行init 方法 /WEB-INF/cxf-servlet.xml 配置文件 //request String basePath=http://localhost:8080/cxfspringweb/ws/helloService JaxWsServerFactoryBean bean=new JaxWsServerFactoryBean(); bean.setAddress(basePath);//設置服務的訪問地址 bean.setServerClass(cn.itcast.cxf.spring.HelloService.class);//設置服務的接口 bean.setServerBean(Class.for("cn.itcast.cxf.spring.HelloService").newInstance());//設置服務的接口實現類 bean.create();//發布 --> </init-param> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

cxf-servlet.xml配置以下

<?xml version="1.0" encoding="UTF⑻"?> <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⑶.0.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"> <!-- 引入CXF Bean定義以下,初期的版本中使用 --> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!-- 通過配置文件的方式發布1個不帶接口的webservice --> <!-- 1,id,2,服務對外的訪問地址,3,提供服務的實現類.. --> <jaxws:endpoint id="helloService" address="/helloService" implementor="cn.itcast.cxf.spring.HelloService"></jaxws:endpoint> <!-- 通過此配置發布1個帶接口的webservice --> <!-- 1,id 2,服務對外的訪問地址 3,接口的類型 --> <jaxws:server id="makeCallService" address="/makeCallService" serviceClass="cn.itcast.cxf.spring.call.CallService"> <jaxws:serviceBean> <!-- 接口的實現類... --> <bean class="cn.itcast.cxf.spring.call.CallServiceImpl"></bean> </jaxws:serviceBean> <!-- 通過配置文件的方式添加攔截器。。。 --> <!-- 添加要求的消息攔截器 --> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> </jaxws:inInterceptors> <!-- 添加響應的消息攔截器.. --> <jaxws:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> </jaxws:outInterceptors> </jaxws:server> </beans>

客戶端調用

  1. 用wsdl2java生成客戶端代碼
  2. 編寫spring配置文件
  3. 調用

spring配置文件

<?xml version="1.0" encoding="UTF⑻"?> <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⑶.0.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"> <!-- 引入CXF Bean定義以下,初期的版本中使用 --> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!-- 1,id,通過id 獲得到bean 2,訪問webservice 的服務的地址 3,需要依賴接口類型 --> <jaxws:client id="itcast" address="http://localhost:8080/cxfspringweb/ws/makeCallService" serviceClass="cn.itcast.cxf.spring.call.CallService"></jaxws:client> </beans>

調用

package cn.itcast.spring.client; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.cxf.spring.call.CallService; /** * * 通過配置文件的方式調用webservice, * 同時也需要依賴1個接口.... * * @ 作者 zhuwu@itcast.cn * */ public class SpringClientInvoke { /** * @param args */ public static void main(String[] args) { //解析spring配置文件 ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml"); //通過getBean 拿到接口 的實例對象... CallService callService= (CallService) context.getBean("itcast"); boolean flag=callService.makeCaller("zj"); System.out.println(flag); } }

ajax調用webService實現

<%@ page language="java" contentType="text/html; charset=UTF⑻" pageEncoding="UTF⑻"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; System.out.println(basePath); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="js/jquery⑴.6.2.js"></script> <script type="text/javascript"> /** ajax 的xmlHttpRequest 對象可以發送1個http 要求 我們可以把服務端需要的xml 格式的數據傳送到服務端。 摹擬soap 要求。 **/ var itcast; itcast={ sendMessage:function(){ var data='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://call.spring.cxf.itcast.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'; data+='<soapenv:Body><q0:makeCaller><arg0>itcast</arg0></q0:makeCaller></soapenv:Body></soapenv:Envelope>'; $.ajax({ url: "ws/makeCallService", data:data, contentType:'text/xml;charset=utf⑻', type:"POST", dataType:"xml", success: function(data, textStatus, jqXHR){ var text=$(data).find("return").text(); $(".message").html(text); // $(".message").show(3000); $(".message").slideDown(3000); } }); }, hide:function(){ $(".message").slideUp(3000); } } </script> </head> <body> <input type="button" value="顯示" onclick="itcast.sendMessage()"/> <input type="button" value="隱藏" onclick="itcast.hide()"/> <div class="message" style="border-width: 4xp;border-style: solid;border-color: red;width: 400px;height: 400px;display: none;"> </div> </body> </html>

使用JS訪問WebService 跨域

通過js來訪問webservice有兩種不同的情勢
1、通過SOAP協議進行訪問。
發送的全部是XML數據,且必須是POST要求。
2、通過HTTP的get/post方式進行訪問。
此種情況又分成不同的情勢,此種情況必要在cxf下發布。由于jdk1.6基本的發布不支持Http,soap1.2。
1、發送和接收XML數據。

JS1直存在跨域訪問的問題

目前的jQuery不支持跨域訪問。如果要進行訪問必須使用jQuery的jsonp數據情勢。
但原始的ajax可以通過get/post方式跨域訪問http上的資源。
以下是通過jaxb發布的webservice。并通過js實現訪問webService.

  • 第1步:書寫1個webService,通過Endpoint端點服務發布。
  • 第2步:書寫XMLHttpRequest的ajax對像。
  • 第3步:想法獲得要求webService的XML數據和WebService返回的數據,以便于數據解析。
  • 第4步:書寫代碼

第1步:書寫webService的服務:

這里寫圖片描述

第2步:創建XMLHttpRequest對像:

這里寫圖片描述

第3步:想法獲得SOAP協議的文本,并在JS中做為發出的XML數據

這里寫圖片描述

CRUD-Server:

這里寫圖片描述

CRUD-Client:
這里寫圖片描述

這里寫圖片描述

總結

散布與接收webService的方法

這里寫圖片描述

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 五月天婷婷丁香 | 国产日韩在线视频 | 51久久夜色精品国产麻豆 | 男女的隐私视频网站 | 欧美一区二区三区在线看 | 久久久久久九九 | 91精品国产高清一区二区三区 | 成人1区2区 | 日韩精品一区在线 | 不卡av电影在线 | 一级黄色毛片 | 99久久久国产精品免费调教网站 | 久久国产一区二区三区 | 国产日韩成人 | 国产九九热 | 一级久久 | 国产剧情在线观看一区 | 中文字幕一区二区三区日韩精品 | 中文字幕偷拍 | 中文字幕日韩欧美一区二区三区 | 成人一区二区三区 | 国产二区精品 | 中文字幕一区在线观看视频 | 99午夜 | 91精品区| 亚洲香蕉影院 | 精品国产1区2区 | 一区二区在线视频 | 精品久久久免费 | 国精品一区二区 | 中文字幕免费中文 | 在线亚洲一区二区 | 久久性色 | 高清免费毛片 | 精品一区久久久 | 成人免费视频国产 | 在线日韩视频 | 精品专区| 久久久国产精品一区二区三区 | 免费观看一区二区三区毛片 | 精品九九|