使用Spring的ServletEndpointSupport实现WebService接口范例

更新时间:2023-03-08 18:02:16 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

使用Spring的ServletEndpointSupport实现WebService接口范例

一、环境配置 :在 eclipse 中配置引入相应的 Spring 框架( core/Remoting/Web )、 axis 包。

二、代码开发

1、 在 MyEclipse 中建立一个新的 J2EE 的 Web Project, 新建 java 包 test 。

2、 接口文件 HelloWorldRemote.java

package test;

//Spring 工程中要使用的接口文件 public interface HelloWorldRemote {

public String getMessage(String name); }

3、 接口实现文件 HelloWorldBean.java

package test;

//Spring 工程中要使用的接口实现文件

public class HelloWorldBean implements HelloWorldRemote {

private String helloStr; // Spring 中需要注入的字符串 public String getHelloStr() {

return helloStr; }

public void setHelloStr(String helloStr) {

this.helloStr = helloStr; }

// 实现接口中的方法

public String getMessage(String name) {

return helloStr + \ } }

4、 在 Spring 中对 Web Service 进行封装很简单,仅仅需要继承

org.springframework.remoting.jaxrpc.ServletEndpointSupport 类,实现里面的一些方法,包装一次,将其发布出来就可以。 HelloWorldWebService.java

package test;

import javax.xml.rpc.ServiceException;

import org.springframework.remoting.jaxrpc.ServletEndpointSupport; public

class

HelloWorldWebService

extends

ServletEndpointSupport

implements

HelloWorldRemote {

private HelloWorldRemote helloWorld; protected void onInit() throws ServiceException {

// 在 Spring 容器中获取 Bean 的实例

helloWorld = (HelloWorldRemote) getApplicationContext() .getBean(\ }

public String getMessage(String name) {

// 执行 Bean 中的相同的方法 return helloWorld.getMessage(name); } }

三、配置文件 (全部放在 /WEB-INF/ 目录下 )

1、 web.xml 为 web 加载 spring 和 axis 配置

contextConfigLocation

/WEB-INF/applicationContext.xml

context

org.springframework.web.context.ContextLoaderServlet

1

axis

org.apache.axis.transport.http.AxisServlet

2

axis /services/*

2、 applicationContext.xml 为 spring 的配置

Say Hello to :

3、 server-config.wsdd 为 axis 服务配置

xmlns:java=\

type=\

http://xml.apache.org/axis/wsdd/

value=\

四、测试 客户端 TestWebServiceClient.java

package test;

import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class TestWebServiceClient {

public static void main(String[] args) { try {

String wsdlUrl

= \ String nameSpaceUri

= \ // 创建调用对象

Service service = new Service(); Call call = null;

call = (Call) service.createCall(); // 调用 getMessage

System.out.println(\

call.setOperationName(new QName(nameSpaceUri, \ call.setTargetEndpointAddress(new java.net.URL(wsdlUrl)); String ret = (String) call.invoke(new Object[] { \ System.out.println(\ }

catch (Exception e) {

e.printStackTrace(); } } } 本

CSDN

http://blog.csdn.net/thinker28754/archive/2008/04/23/2318236.aspx

在spring中利用axis工具配置webservice成功案例

第一步:确认你用到的spring bean存在并且有效 比如 inviteService 第二步:创建要发布的服务方法 /** * */

package com.chinamobile.survey.webservice;

import java.util.List;

import org.springframework.remoting.jaxrpc.ServletEndpointSupport; import com.chinamobile.survey.entity.vo.ExamPlanContainer; import com.chinamobile.survey.invite.business.IInviteService; /**

* @author jianqiang.jiang * */

public class InviteEndPoint extends ServletEndpointSupport implements IInviteService {

// 将真实的业务bean包装成WebService private IInviteService inviteService;

// 该方法由Spring调用,将目标业务bean注入。 protected void onInit() {

this.inviteService = (IInviteService) getWebApplicationContext() .getBean(\ }

// 将业务bean的业务方法暴露成WebService

public int getAllowAnswerExamPlanCount(long userId) throws Exception { return inviteService.getAllowAnswerExamPlanCount(userId); }

public ExamPlanContainer getAllowAnswerExamPlanContainer(long userId, long offset, long maxRow) throws Exception {

return inviteService.getAllowAnswerExamPlanContainer(userId, offset, maxRow); }

}

注意里面的 ServletEndpointSupport 和对bean的引用 第三步.编辑两个文件 1.web.xml中 增加

AxisServlet

org.apache.axis.transport.http.AxisServlet

AxisServlet /services/*

2.生成server-config.wsdd,放在WEB-INF下

type=\

value=\

value=\/>

languageSpecificType=\ainer\

注意其中的 返回类型注册, 第四步.

生成客户端测试代码:

package com.chinamobile.survey.webservice; import java.util.Iterator; import java.util.List;

import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType;

import org.apache.axis.encoding.ser.BeanDeserializerFactory; import org.apache.axis.encoding.ser.BeanSerializerFactory; import com.chinamobile.survey.entity.vo.ExamPlanContainer; import com.chinamobile.survey.entity.vo.WsExamPlan; public class ArchiveClient {

public static void main(String args[]) throws Exception { try {

String endpoint = \ Service service = new Service(); Call call = (Call) service.createCall();

QName searchresultqn = new QName(\ \

Class searchresultcls = ExamPlanContainer.class; call

.registerTypeMapping(searchresultcls, searchresultqn, new BeanSerializerFactory(searchresultcls, searchresultqn),

new BeanDeserializerFactory(searchresultcls, searchresultqn));

QName wsExamPlanQN = new QName(\ Class wsepcls = WsExamPlan.class;

call.registerTypeMapping(wsepcls, wsExamPlanQN,

new BeanSerializerFactory(wsepcls, wsExamPlanQN), new BeanDeserializerFactory(wsepcls, wsExamPlanQN)); call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName(new QName(\ \

call.addParameter(\ call.addParameter(\ call.addParameter(\ call.setReturnType(searchresultqn);

ExamPlanContainer result = (ExamPlanContainer) call .invoke(new Object[] { new Long(1), new Long(0), new Long(1000) }); if (result != null) {

List list = result.getExamPlanList();

for (Iterator iter = list.iterator(); iter.hasNext();) { WsExamPlan wsExamPlan = (WsExamPlan) iter.next(); System.out.println(wsExamPlan.getName()); } }

} catch (javax.xml.rpc.ServiceException e) { e.printStackTrace();

} catch (java.net.MalformedURLException e) {

e.printStackTrace();

} catch (java.rmi.RemoteException e) { e.printStackTrace(); } }

public static void testCount() { try {

String endpoint = \ Service service = new Service(); Call call = (Call) service.createCall();

call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName(new QName(\ \

call.addParameter(\ call.setReturnType(XMLType.XSD_INT); Integer result = (Integer) call

.invoke(new Object[] { new Long(1) }); System.out.println(result);

} catch (javax.xml.rpc.ServiceException e) { e.printStackTrace();

} catch (java.net.MalformedURLException e) { e.printStackTrace();

} catch (java.rmi.RemoteException e) { e.printStackTrace(); } } }

第五步:get方式访问测试

http://localhost:8080/exam/services/ExamService?method=getAllowAnswerExamPlanContainer¶mter0=1¶mter1=1¶mter2=1

建webservice服务的各种框架(axis、axis2、xifre) 文章分类:Java编程

此篇文章未完成,请稍后在查看。本来是保存为草稿的,可是又编辑几次后,不知道怎么就发布了。第一次写blog,大家别拍砖。。。

最近项目用了webservice,调研了几个框架(axis、axis2、xifre),现在分别来说明下这几个框架的使用及优缺点。

1、axis。

axis是这几个建webservice服务框架中,速度最慢、配置最复杂的一个,也难怪Apache要放弃它了。

首先在eclipse中建立一个web工程,取名为WebService-axis,建一个包test,在此包下建一个类Hello,Hello代码如下: Java代码

1. public class Hello {

2. public String sayHello(String name) { 3. return \

4. } 5. }

public class Hello {

右键点击这个类,“Web Services”——>“Create Web service”,出现如图:

然后点N下“Next”(其中有步要先点“start server”,要不“next”为灰不可用)。

完成之后,eclipse便帮我们将服务端和客户端都生成了,我们来看看都生成了哪些代码。 首先,服务端目录结构:

在集成Spring + Axis 的环境下webservice的发布和部署 2008-03-04 18:22 开发中遇到两个难点:

1. 在集成Spring + Axis 的环境下webservice的发布和部署; 2. 文件上传和下载的commons-fileupload-1.2的使用。 下面分别谈这两个问题.

一. 在集成Spring + Axis 的环境下webservice的发布和部署 1.1 DEMO文件样例 1.1.1 项目结构图:

本文来源:https://www.bwwdw.com/article/blj6.html

Top