springmvc+spring+hibernate集成cxf
生活随笔
收集整理的這篇文章主要介紹了
springmvc+spring+hibernate集成cxf
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先說一下背景,我們的系統是springmvc+spring+hibernate,已經能正常運行,現在要開發webservice,選用cxf。
1、maven依賴
2、web.xml配置
在配置好ssh的基礎上添加如下cxf的配置:
原因是因為cxf的spring容器必須使用ContextLoadListener來啟動,而spring MVC采用的DispatcherServlet來啟動的,所以無法加載cxf的服務,于是采用 博文中的方法,分開加載,只將cxf的配置剝離出來。
3、spring-cxf.xml的配置
4、java的編寫
1、定義接口
2、實現接口
package com.newtouch.iesc.ma.webservice;import javax.jws.WebParam; import javax.jws.WebService;/*** @Description: java類作用描述* @Author: PZHOU* @CreateDate: 2019-4-26$ 14:35$*/ @WebService(endpointInterface="com.newtouch.iesc.ma.webservice.HelloService",targetNamespace="http://webservice.ma.iesc.newtouch.com/") public class HelloServiceImp implements HelloService{@Overridepublic String sayHello(@WebParam(name = "userName",targetNamespace="http://webservice.ma.iesc.newtouch.com/") String userName) {StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");sb.append("<userBean>");sb.append("<userName>" + userName + "</userName>");sb.append("</userBean>");return sb.toString();} }5、啟動項目:
訪問:http://localhost:8080/項目名/webservice/
出現如下頁面,則部署成功:
點擊鏈接會出現如下頁面:
大功告成,服務端發布成功!!!
如果你現在覺得已經完成的,那就說明你太嫰了,因為客戶端才是大頭,因為一個客戶端,我花了兩天時間。
客戶端開發:(asix框架)
客戶端代碼:
package com.newtouch.iesc.ma.utils;import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceException;import org.apache.axis.Constants; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType;/*** 調用webService的工具類*/ public class CallWebServiceUtil {/*** 取得webservice操作** @param strXml* :傳入參數,為xml的類型* @param method* :webservice 方法名* @param url* :wsdl路徑* @param nameSpace* :webservice命名空間* @param parameter* :傳入方法的參數名,只能為一個* @return*/public static Object getWebService(String strXml, String method, String url, String nameSpace, String[] parameter) {Service service = new Service();Object obj = null;Call call;try {// 創建連接call = (Call) service.createCall();// 設置webservice地址call.setTargetEndpointAddress(new URL(url));// 設置調用的方法名(包含方法所在的命名空間)call.setOperationName(new QName(nameSpace, method));// 設置調用時需要傳入的參數(包含參數所屬方法所對應的命名空間)call.addParameter(new QName(nameSpace, parameter[0]),XMLType.XSD_STRING,ParameterMode.IN);call.setUseSOAPAction(true);// 設置返回參數類型call.setReturnType(Constants.XSD_STRING);// 設置操作方法完整路徑(包含命名空間與方法名)call.setSOAPActionURI(nameSpace + method);// 執行調用過程并傳入調用時所需參數obj = call.invoke(new Object[] { strXml });} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ServiceException e) {// TODO Auto-generated catch blocke.printStackTrace();}return obj;}}調用客戶端:
package com.newtouch.iesc.ma;import com.newtouch.iesc.ma.utils.CallWebServiceUtil; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType;import javax.jws.WebService; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode;/*** 使用axis調用cxf發布的webservice接口* @author moon**/ @WebService(targetNamespace = "http://webservice.ma.iesc.newtouch.com/") public class CxfClient {private static String getXml(String name) {StringBuffer sb = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");sb.append("<userBean>");sb.append("<userName>" + name + "</userName>");sb.append("</userBean>");return sb.toString();}public static void main(String[] args) throws Exception {Object object = CallWebServiceUtil.getWebService(getXml("zhou"),"sayHello","http://localhost:8080/account_war_exploded/webService/sayHello?wsdl","http://webservice.ma.iesc.newtouch.com/",new String[]{"userName"});System.out.print(object.toString());} }測試,如果成功,就代表你能讓其他人調用,并你也能調用別人。
總結
以上是生活随笔為你收集整理的springmvc+spring+hibernate集成cxf的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pom详解
- 下一篇: EXCEL导入导出使用的框架