1. 程式人生 > >spring4集成cxf3的服務端/客戶端使用

spring4集成cxf3的服務端/客戶端使用

classpath -- xmla point 網上 user 生成 smtp 關於

Web Service = SOAP + HTTP + WSDL。其中,SOAP Simple Object Access Protocol)協議是web service的主體,它通過HTTP或者SMTP等應用層協議進行通訊,自身使用XML文件來描述程序的函數方法和參數信息,從而完成不同主機的異構系統間的計算服務處理。這裏的WSDL(Web Services Description Language)web 服務描述語言也是一個XML文檔,它通過HTTP向公眾發布,公告客戶端程序關於某個具體的 Web service服務的URL信息、方法的命名,參數,返回值等。。。。。。。。

參考鏈接:http://blog.csdn.net/longwei000/article/details/50592242

本文重在應用,在https://github.com/neugle/spring_mvc_cxf上的代碼修改之,謝謝作者,網上一大堆示例代碼,卻鮮有亮個完整例子的。

 1.server端

  註意在idea上記得在用Project Structure將resource標註為maven工程能識別的Resource模塊。不然配置文件會找不到。

  根據它的mapper文件建立相應數據庫(就一個t_lawyer表),造幾條記錄。

  用tomcat8.5將它運行起來,訪問路徑為http://localhost:8092/webservice/testService

 2.client端

  1)下載apach-cxf,配置其環境變量。

  2)wsdl2java -keep http://localhost:8092/webservice/testService?wsdl用於使用wsdl2java命令生成客戶端代碼。

  3)copy原工程,刪除java代碼,將上面生成的客戶端代碼copy過來。

  4)將spring-cxf.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <!-- CXF webservice配置 --> <!-- CXF3以後,只需要引入這個配置文件即可,其他兩個廢棄掉了--> <import resource="classpath:META-INF/cxf/cxf.xml"/> <!-- WebService --> <!--<jaxws:endpoint id="testService" implementor="com.rain6.cxf.service.impl.TestWebServiceImpl" address="/testService"/>--> <jaxws:client id="userClient" serviceClass="com.rain6.cxf.service.TestWebService" address="http://localhost:8092/webservice/testService"> </jaxws:client> </beans>

  5)寫一個Client.java調用服務端方法

  

import com.rain6.cxf.service.Lawyer;
import com.rain6.cxf.service.TestWebService;import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Client {
    public static void main (String[] strings){
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:spring/spring-cxf.xml");
        TestWebService testWebService = (TestWebService) classPathXmlApplicationContext.getBean("userClient");
        Lawyer lawyer = testWebService.selectByPrimaryKey("143347");
        System.out.println("----------------->>>>>>>>>>>>>>>>>client calling .......");
        System.out.println(lawyer.toString());
    }
}

OK了!

spring4集成cxf3的服務端/客戶端使用