開(kāi)發(fā)中遇到兩個(gè)難點(diǎn): 1. 在集成Spring + Axis 的環(huán)境下webservice的發(fā)布和部署; 2. 文件上傳和下載的commons-fileupload-1.2的使用。 下面分別談這兩個(gè)問(wèn)題. 一. 在集成Spring + Axis 的環(huán)境下webservice的發(fā)布和部署 1.1 DEMO文件樣例 1.1.1 項(xiàng)目結(jié)構(gòu)圖: (為了尊重原作者,我沒(méi)有替換包名) 1.1.2 項(xiàng)目代碼介紹 1.1.2.1 依次建立一下代碼文件 ------------------------------------------------------------------------------------ package com.test.www; import java.rmi.RemoteException; /** * @author fhway * */ public interface IHelloWorld { public String getMessage(String name) throws RemoteException; } package com.test.www.impl; import com.test.www.IHelloWorld; /** * @author fhway */ public class HelloWorldImpl implements IHelloWorld { private String helloStr; // Spring中需要注入的字符串 /** * 實(shí)現(xiàn)接口中的方法,得到Say Hello to <somebody>的字符串 */ public String getMessage(String name) { return helloStr + ":" + name; } public String getHelloStr() { return helloStr; } public void setHelloStr(String helloStr) { this.helloStr = helloStr; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } } package com.test.www.webservice; /** * @author fhway */ import java.rmi.RemoteException; import javax.xml.rpc.ServiceException; import org.springframework.remoting.jaxrpc.ServletEndpointSupport; import com.test.www.IHelloWorld; public class JaxRpcHelloWorld extends ServletEndpointSupport implements IHelloWorld { private IHelloWorld helloWorld; protected void onInit() throws ServiceException { // 在Spring容器中獲取Bean的實(shí)例 helloWorld = (IHelloWorld) getApplicationContext() .getBean("helloWorld"); } public String getMessage(String name) throws RemoteException { // 執(zhí)行Bean中的相同的方法 return helloWorld.getMessage(name); } } 基于以上文件,不用多做解釋 1.1.3 用Myeclipse加入【Add Spring Capabilities……】
得到文件applicationContext.xml 并放在WEB-INF 下面。內(nèi)容如下: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www./dtd/spring-beans.dtd"> <beans> <bean id="helloWorld" class="com.test.www.impl.HelloWorldImpl"> <property name="helloStr"> <value>Say Hello to :</value> </property> </bean> </beans> 這點(diǎn)和原文的照本宣課可能不一樣。我沒(méi)有配置XXXX.service.xml文件 在該文件中注入一個(gè)bean的參數(shù):【Say Hello to :】 用于輸出的時(shí)候使用。 1.1.4 配置web.xml 文件如下: <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java./xml/ns/j2ee" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://java./xml/ns/j2ee http://java./xml/ns/j2ee/web-app_2_4.xsd"> <display-name>HelloWorld_SpringAxisSample</display-name> <!-- Spring框架需要引入的配置文件及相關(guān)類(lèi) --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <servlet> <servlet-name>context</servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet> <display-name>Apache-Axis Servlet</display-name> <servlet-name>AxisServlet</servlet-name> <servlet-class> org.apache.axis.transport.http.AxisServlet </servlet-class> </servlet> <servlet> <display-name>Axis Admin Servlet</display-name> <servlet-name>AdminServlet</servlet-name> <servlet-class> org.apache.axis.transport.http.AdminServlet </servlet-class> <load-on-startup>100</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/servlet/AxisServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>*.jws</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AdminServlet</servlet-name> <url-pattern>/servlet/AdminServlet</url-pattern> </servlet-mapping> <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> </web-app> 后段關(guān)于axis的部分在你發(fā)布wsdl的時(shí)候也會(huì)自動(dòng)生成的,所以現(xiàn)帖出來(lái); 在配置【org.springframework.web.context.ContextLoaderServlet】的上下文時(shí),我們可以配置一個(gè)Servlet; 也可以做一個(gè)【listener】 <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> 1.1.5 利用wtp平臺(tái)提供的自動(dòng)話生成工具發(fā)布你的WebService 你的項(xiàng)目文檔中將會(huì)產(chǎn)生以下幾個(gè)文件夾或文件: JaxRpcHelloWorldService server-config.wsdd wsdl axRpcHelloWorld.wsdl 1.1.6 如果你在發(fā)布的時(shí)候啟動(dòng)了你的Tomcat 或WebLogic 那么你就會(huì)看見(jiàn)以下的畫(huà)面 IE: = http://localhost:8088/HelloWorld_SpringAxisSample/services/JaxRpcHelloWorld
IE: = http://localhost:8088/HelloWorld_SpringAxisSample/services/JaxRpcHelloWorld?wsdl
1.1.7 編寫(xiě)測(cè)試類(lèi) package com.test.www.webservice; import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class TestWebServiceClient { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { String wsdlUrl = "http://localhost:8088/HelloWorld_SpringAxisSample/services/JaxRpcHelloWorld?wsdl"; String nameSpaceUri = "http://localhost:8088/HelloWorld_SpringAxisSample/services/JaxRpcHelloWorld"; // 創(chuàng)建調(diào)用對(duì)象 Service service = new Service(); Call call = null; call = (Call) service.createCall(); // 調(diào)用sayHello System.out.println(">>>getMessage"); /* Webservice name; methd name */ call.setOperationName(new QName(nameSpaceUri, "getMessage")); call.setTargetEndpointAddress(new String(wsdlUrl)); String ret = (String) call.invoke(new Object[] { "ABC" }); System.out.println("return value is " + ret); } catch (Exception e) { e.printStackTrace(); } } } 我的tomcat為8088 可能和你的不一樣,請(qǐng)注意修改 1.1.8 運(yùn)行測(cè)試類(lèi),結(jié)果如下:
1.1.9 Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1593309 |
|
來(lái)自: 不會(huì)游泳的魚(yú) > 《Spring》