Xfire簡介
XFire是新一代的Java WebService引擎,XFire使得在JavaEE應用中發佈Web服務變得輕而易舉。和其他Web服務引擎相比,XFire的配置非常簡單,可以非常容易地和Spring集成,它使得Java開發人員終於可以獲得和.Net開發人員一樣的開發效率。
XFire是codeHaus組織提供的一個開源框架,它構建了POJO和SOA之間的橋樑,主要特性就是支持將POJO通過非常簡單的方式發佈成Web服務,這種處理方式不僅充分發揮了POJO的作用,簡化了Java應用轉化爲Web服務的步驟和過程,也直接降低了SOA的實現難度,爲企業轉向SOA架構提供了一種簡單可行的方式。
XFire 支持將Web服務綁定到POJO、XMLBeans、JAXB1.1、JAXB2.0和Castor;
支持基於HTTP、JMS、XMPP等多種協議訪問Web服務;
支持多種Web服務業界重要標準如SOAP、WSDL、Web服務尋址(WS-Addressing)、Web服務安全(WS-Security)等;
支持JSR181,可以通過JDK5配置Web服務;
高性能的SOAP實現;
服務器端、客戶端代碼輔助生成;
對Spring、Pico、Plexus等項目的支持等。
XFire是一種基於Servlet技術的SOA應用開發框架,需要Servlet容器的支持。XFire支持在多種Servlet容器中運行,包括Websphere、Weblogic、TOMCAT等。支持JDK 1.4以上(含1.4).
XFire框架目前的最新版本是1.2.6,可以訪問http://xfire.codehaus.org 下載XFire框架的安裝包,下載時請選擇“全部二進制發佈包”,而不僅僅是“XFirejar文件".下載地址:http://xfire.codehaus.org/Download
Xfire已經不更新了,所以目前Spring4.0及以上已經不支持Xfire,如果需要和Spring集成,就用Spring3.2.x吧,當然也可以選擇CXF(Xfire的下一代),新入職一家公司用的是Xfire,就簡單看了一下。
快速發佈WebService接口
1.Idea新建一個Spring + Web項目
2.將下載的Xfire包導入項目
3.確保jar包在發佈的war包根目錄的lib目錄下
4.編寫暴露方法的接口和實現類,如圖示,具體代碼就不粘貼了,就是寫一個簡答接口和實現類就行
5.修改web.xml文件,目的是註冊servlet, 發佈暴露WSDL
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>xfireSpringServlet</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfireSpringServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
6.修改spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<import resource="classpath*:org/codehaus/xfire/spring/xfire.xml"/>
<bean id="BookService" class="org.codehaus.xfire.spring.remoting.XFireExporter">
<property name="serviceFactory" ref="xfire.serviceFactory"></property>
<property name="xfire" ref="xfire"></property>
<property name="serviceBean" ref="BookServiceImpl"></property>
<property name="serviceClass" value="com.jett.service.BookService"></property>
<property name="name" value="BookService"></property>
</bean>
<bean id="BookServiceImpl" class="com.jett.service.impl.BookServiceImpl"></bean>
<bean id="FoodService" class="org.codehaus.xfire.spring.remoting.XFireExporter">
<property name="serviceFactory" ref="xfire.serviceFactory"></property>
<property name="xfire" ref="xfire"/>
<property name="serviceBean" ref="FoodServiceImpl"/>
<property name="serviceClass" value="com.jett.service.FoodService"/>
<property name="name" value="FoodService"/>
</bean>
<bean id="FoodServiceImpl" class="com.jett.service.impl.FoodServiceImpl"></bean>
</beans>
7. 啓動TOMCAT,
訪問地址: http://localhost:8080/應用根/service 查看wsdl
如果能查看到wsdl,服務端就OK了
如果項目在添加Maven框架,就不需要自己下載jar包,只需要添加如下依賴就好了
<dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-core</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-spring</artifactId>
<version>1.2.6</version>
</dependency>
當然最後需要保證自動下載的jar要打包發佈到war中,不然在tomcat上找不到相應的jar包
客戶端訪問
客戶端可以有多種方式訪問,比如下載生成客戶端啊,我這邊就介紹客戶端動態訪問的方式
客戶端也需要Xfire的jar,可以自己導入或者maven導入,這裏給出訪問代碼
import org.codehaus.xfire.client.Client;
import java.net.URL;
/**
* @Author: Jett
* @Date: 2019/4/4 10:10
* Desciption:
*/
public class FoodTest {
public static void main(String[] args) throws Exception {
Client client = new Client(new URL("http://localhost:8080/xfireTest/services/FoodService?wsdl"));
Object[] objects = client.invoke("food", new Object[]{"apple", 2});
System.out.println(objects[0]);
}
}
這樣一個簡單的demo就實現了,深入的關於傳參,傳對象的就大家自己深入挖掘吧