您的位置:首页 > 编程语言 > Java开发

在集成Spring + Axis 的环境下webservice的发布和部署

2007-04-30 14:54 381 查看
开发中遇到两个难点:

1. 在集成Spring + Axis 的环境下webservice的发布和部署;

2. 文件上传和下载的commons-fileupload-1.2的使用。

下面分别谈这两个问题.

一. 在集成Spring + Axis 的环境下webservice的发布和部署

1.1 DEMO文件样例

1.1.1 项目结构图:





(为了尊重原作者,我没有替换包名)

1.1.2 项目代码介绍

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中需要注入的字符串

/**

* 实现接口中的方法,得到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的实例

helloWorld = (IHelloWorld) getApplicationContext()

.getBean("helloWorld");

}

public String getMessage(String name) throws RemoteException {

// 执行Bean中的相同的方法

return helloWorld.getMessage(name);

}

}

基于以上文件,不用多做解释

1.1.3 用Myeclipse加入【Add Spring Capabilities……】



得到文件applicationContext.xml 并放在WEB-INF 下面。内容如下:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/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>

这点和原文的照本宣课可能不一样。我没有配置XXXX.service.xml文件 在该文件中注入一个bean的参数:【Say Hello to :】 用于输出的时候使用。

1.1.4 配置web.xml 文件如下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_ID" version="2.4"

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>HelloWorld_SpringAxisSample</display-name>

<!-- Spring框架需要引入的配置文件及相关类 -->

<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>

后段关于axis的部分在你发布wsdl的时候也会自动生成的,所以现帖出来;

在配置【org.springframework.web.context.ContextLoaderServlet】的上下文时,我们可以配置一个Servlet; 也可以做一个【listener】

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

1.1.5 利用wtp平台提供的自动话生成工具发布你的WebService

你的项目文档中将会产生以下几个文件夹或文件:

JaxRpcHelloWorldService

server-config.wsdd

wsdl

axRpcHelloWorld.wsdl

1.1.6 如果你在发布的时候启动了你的Tomcat 或WebLogic 那么你就会看见以下的画面

IE: = http://localhost:8088/HelloWorld_SpringAxisSample/services/JaxRpcHelloWorld



IE: = http://localhost:8088/HelloWorld_SpringAxisSample/services/JaxRpcHelloWorld?wsdl



1.1.7 编写测试类

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";

// 创建调用对象

Service service = new Service();

Call call = null;

call = (Call) service.createCall();

// 调用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 可能和你的不一样,请注意修改

1.1.8 运行测试类,结果如下:



1.1.9
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: