日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Webservice入门教程_用CXF编写基于Spring的WebService

發布時間:2025/3/19 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Webservice入门教程_用CXF编写基于Spring的WebService 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

項目專欄:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/column/info/37726

實現

搭建服務端

1.打開Eclipse--新建Dynamic Project--CXF_Spring_Web

2.導入cxf的jar包

jar包位置:

將lib下的所有jar包文件導入,雖然不都用得到但是先暫時都導入。

然后右擊lib包右擊build path

apache-cxf-2.5.9下載:

https://download.csdn.net/download/badao_liumang_qizhi/11171320

3.新建接口以及實現類

在src下新建包,先在包下新建實體類

package com.badao.bean;public class Order {public int getId() {return id;}public void setId(int id) {this.id = id;}public Order(int id, String name, double price) {super();this.id = id;this.name = name;this.price = price;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}private int id;private String name;private double price; }

然后在包下新建接口OrderWS

package com.badao.cxf;import javax.jws.WebMethod; import javax.jws.WebService;import com.badao.bean.Order; @WebService public interface OrderWS {@WebMethodpublic Order getOrderById(int id); }

接口上要添加注解@WebService

方法上要添加注解?@WebMethod

然后新建實現類

package com.badao.cxf;import javax.jws.WebService;import com.badao.bean.Order; @WebService public class OrderWSImpl implements OrderWS {@Overridepublic Order getOrderById(int id) {System.out.println("服務端獲取的id為:"+id);return new Order(id,"訂單1",100000.0);}}

接口實現上要添加注解@WebService

4.編寫配置文件beans.xml

依據文檔,需要編寫一個beans.xml配置文件。

文檔下載位置:

https://download.csdn.net/download/badao_liumang_qizhi/11153561

beans.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.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.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" /><!--終端:發布webservice(將SEI的實現類對象與web service所提供的網絡地址關聯)address="/orderWS":配置虛擬路徑--><jaxws:endpoint id="orderWS" implementor="com.badao.cxf.OrderWSImpl"address="/orderWS" /></beans>

5.修改web.xml

依據文檔

web.xml代碼:

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/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_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>CXF_Spring_Web</display-name><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><!-- 配置上下文初始化參數:指定cxf的spring的beans.xml --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:beans.xml</param-value></context-param><!-- 加載上面參數的配置文件的listener --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 匹配所有請求,將請求先交給cxf框架處理 --><servlet><servlet-name>CXFServlet</servlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>CXFServlet</servlet-name><url-pattern>/*</url-pattern></servlet-mapping> </web-app>

運行服務端

將項目部署到Tomcat上。

打開瀏覽器訪問:

http://localhost:8080/CXF_Spring_Web/orderWS

查看wsdl文件

打開瀏覽器訪問:

http://localhost:8080/CXF_Spring_Web/orderWS?wsdl

至此則服務端部署完成。

服務端項目結構目錄

客戶端實現

1.新建項目

Eclipse新建Dynamic project

同上面一樣導入jar包。

2.根據wsdl文檔生成客戶端代碼

下載apache-cxf-2.5.9,在bin目錄下有wsdl2java.bat

將其配置進環境變量。

進入到客戶端的真實目錄下的src下。

在此處打開cmd,輸入:

wsdl2java http://localhost:8080/CXF_Spring_Web/orderWS?wsdl

可以生成客戶端代碼。

此時回到Eclipse 下刷新項目。

?

編寫客戶端配置文件

依據文檔,在src下新建client-beans.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/jaxws"><!-- serviceClass:是根據服務端wsdl生成代碼的接口的位置address:是上面服務端發布的address--><jaxws:client id="orderClient"serviceClass= "com.badao.cxf.OrderWS"address= "http://localhost:8080/CXF_Spring_Web/orderWS"></jaxws:client> </beans>

編寫客戶端代碼

新建ClientTest.java

package com.badao.test;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.badao.cxf.Order; import com.badao.cxf.OrderWS;public class ClientTest {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]? {"client-beans.xml"});OrderWS orderWS = (OrderWS) context.getBean("orderClient");Order order = orderWS.getOrderById(24);System.out.println("服務端返回的數據為:"+order.getId()+order.getName()+order.getPrice());} }

客戶端項目結構目錄

?

效果

將服務端代碼部署到Tomcat上并運行,然后運行客戶端的ClientTest方法。

客戶端運行效果:

服務端效果:

源碼下載

https://download.csdn.net/download/badao_liumang_qizhi/11174785

?


?

總結

以上是生活随笔為你收集整理的Webservice入门教程_用CXF编写基于Spring的WebService的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。