首先:發佈webService接口流程:
1.導入jar包
XmlSchema-1.1.jar,xfire-all-1.2.6.jar,wsdl4j-1.6.1.jar,org.springframework.web-3.1.1.RELEASE.jar,org.springframework.web.servlet-3.1.1.RELEASE.jar,org.springframework.expression-3.1.1.RELEASE.jar,org.springframework.core-3.1.1.RELEASE.jar,org.springframework.context-3.1.1.RELEASE.jar,org.springframework.beans-3.1.1.RELEASE.jar,org.springframework.asm-3.1.1.RELEASE.jar,org.springframework.aop-3.1.1.RELEASE.jar,jdom-1.0.jar,commons-logging-1.1.1.jar,commons-httpclient-3.0.jar,commons-codec-1.3.jar 加紅吧,期間最頭疼的就是這些jar文件,要不就是找的是錯的,就不就是不全,要不就是xfire與spring的版本衝突,獲取需要條件下載!!!傷心總是如此。另外注意這個xfire的jar包已經全部導入了,不需要再導入其他的xfire的jar包
2.創建提供服務的接口
package com.ljj.service;
public interface BookService {
public String getBook(String xml);
}
3.創建提供服務接口的實現類
package com.ljj.serviceimpl;
import com.ljj.service.BookService;
public class BookServiceImpl implements BookService {
public String getBook(String xml) {
return xml;
}
}
4.配置web.xml文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml</param-value>
</context-param>
<!-- springmvc 上下文監聽器 ContextLoaderListener-->
<servlet>
<servlet-name>xfire</servlet-name>
<servlet-class>
org.codehaus.xfire.spring.XFireSpringServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfire</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>
5.配置spring的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--引入xfire配置-->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<!-- 使用XFire導出器 我們通過XFireExporter將業務類導出爲Web Service,對於任何導出器,
我們都需要引入XFire環境,即serviceFactory和xfire,這是標準的配置。ServiceFactory是
XFire的核心類,它可以將一個POJO生成爲一個Web Service。在本實例中,我們通過定義一個
baseWebService,其餘的webService配置都將該bean作爲父bean,這樣可以簡化Spring的配置,
不需要多次引入serviceFactory和xfire。-->
<bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">
<!-- 引用xfire.xml中定義的工廠 -->
<property name="serviceFactory" ref="xfire.serviceFactory" />
<!-- 引用xfire.xml中的xfire實例 -->
<property name="xfire" ref="xfire" />
</bean>
<!-- 配置業務服務service類 -->
<bean id="bookWS" class="com.ljj.serviceimpl.BookServiceImpl">
</bean>
<bean id="bookService" parent="baseWebService">
<!-- 業務服務bean -->
<property name="serviceBean" ref="bookWS" />
<!-- 業務服務bean的對應接口類 -->
<property name="serviceClass" value="com.ljj.service.BookService" />
</bean>
</beans>
6.測試
啓動服務後在瀏覽器訪問:http://localhost:8080/xfireServer/service/BookService?wsdl看到如下頁面即爲成功
<wsdl:definitions xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.ljj.com" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" targetNamespace="http://service.ljj.com">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.ljj.com">
<xsd:element name="getBook">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="getBookResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="getBookRequest">
<wsdl:part name="parameters" element="tns:getBook"> </wsdl:part>
</wsdl:message>
<wsdl:message name="getBookResponse">
<wsdl:part name="parameters" element="tns:getBookResponse"> </wsdl:part>
</wsdl:message>
<wsdl:portType name="BookServicePortType">
<wsdl:operation name="getBook">
<wsdl:input name="getBookRequest" message="tns:getBookRequest"> </wsdl:input>
<wsdl:output name="getBookResponse" message="tns:getBookResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BookServiceHttpBinding" type="tns:BookServicePortType">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getBook">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="getBookRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getBookResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="BookService">
<wsdl:port name="BookServiceHttpPort" binding="tns:BookServiceHttpBinding">
<wsdlsoap:address location="http://10.1.33.96:8080/xfireServer/service/BookService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
擴展:xfire自定義參數名
1.在接口類裏邊加入註解
@WebService
public interface testService{
@WebMethod
@WebResult(name="resultMsg")
public String getExpertUserId(@WebParam(name="name")String name,@WebParam(name="sex")String sex);
}
2.在接口類相同的文件夾下邊創建testService.aegis.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<mappings>
<mapping>
<method name="方法名">
<parameter index="0" mappedName="參數名" />
<parameter index="1" mappedName="參數名" />
</method>
</mapping>
</mappings>
本地編寫main方法,測試邏輯代碼
public static void main(String[] args) {
org.codehaus.xfire.service.Service srvcModel = new ObjectServiceFactory()
.create(ExpertLoginService.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
.newInstance().getXFire());
String helloWorldURL = "http://192.168.XX.XX:8080/項目名稱/service/ExpertLoginService";
try {
ExpertLoginService srvc = (ExpertLoginService) factory.create(
srvcModel, helloWorldURL);
System.out.print(srvc.getExpertUserId("username", "password"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}