一、环境配置 :在 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 + ":" + name;

}

}

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("myHelloWorldBean");

}

public String getMessage(String name)

{

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

return helloWorld.getMessage(name);

}

}

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

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

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

<!--axis 需要引入的 Servlet -->

<servlet>

<servlet-name>axis</servlet-name>

<servlet-class>

org.apache.axis.transport.http.AxisServlet

</servlet-class>

<load-on-startup>2</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>axis</servlet-name>

<url-pattern>/services/*</url-pattern>

</servlet-mapping>

<!--axis 的 Web Service 的 Web 发布路径 -->

2、  applicationContext.xml 为 spring 的配置

<?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="myHelloWorldBean" class="test.HelloWorldBean">

<property name="helloStr">

<value>Say Hello to :</value>

</property>

</bean>

</beans>

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

<deployment xmlns="http://xml.apache.org/axis/wsdd/"

xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

<handler name="URLMapper"

type="java:org.apache.axis.handlers.http.URLMapper" />

<!-- 系统服务 -->

<service name="AdminService" provider="java:MSG">

<parameter name="allowedMethods" value="AdminService" />

<parameter name="enableRemoteAdmin" value="false" />

<parameter name="className" value="org.apache.axis.utils.Admin" />

<namespace>http://xml.apache.org/axis/wsdd/</namespace>

</service>

<service name="Version" provider="java:RPC">

<parameter name="allowedMethods" value="getVersion" />

<parameter name="className" value="org.apache.axis.Version" />

</service>

<!-- 自定义服务 -->

<service name="myWebService" provider="java:RPC">

<parameter name="className"

value="test.HelloWorldWebService" />

<parameter name="allowedMethods" value="*" />

</service>

<transport name="http">

<requestFlow>

<handler type="URLMapper" />

</requestFlow>

</transport>

</deployment>

 

四、测试 客户端 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

= "http://localhost:8080/spring-axis/services/myWebService?wsdl";

String nameSpaceUri

= "http://localhost:8080/spring-axis/services/myWebService";

// 创建调用对象

Service service = new Service();

Call call = null;

call = (Call) service.createCall();

// 调用 getMessage

System.out.println(">>>getMessage");

call.setOperationName(new QName(nameSpaceUri, "getMessage"));

call.setTargetEndpointAddress(new java.net.URL(wsdlUrl));

String ret = (String) call.invoke(new Object[] { "ABC" });

System.out.println("return value is " + ret);

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

关注流行国外网站

facebook:http://www.fb-on.com

facebook官网:http://www.facebookzh.com

facebook:http://www.cn-face-book.com

youtube:http://www.youtubezh.com

twitter:http://www.twitterzh.com

webservice-之使用axis+spring开发的更多相关文章

  1. 转载 WebService 的CXF框架 WS方式Spring开发

    WebService 的CXF框架 WS方式Spring开发   1.建项目,导包. 1 <project xmlns="http://maven.apache.org/POM/4.0 ...

  2. WebService 的CXF框架 WS方式Spring开发

    1.建项目,导包. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

  3. Java WebService学习笔记 - Axis(一)

    WebService 简介 实际开发中,很多系统都是基于历史遗留系统进行开发,有时,这些系统基于不同的语言,如C,C++,C#,java,PHP等等.为了实现历史系统的再利用,或向外部程序暴露调用接口 ...

  4. 【Spring学习笔记-0】Spring开发所需要的核心jar包

    spring开发所需要的核心jar 1. libs目录下的核心jar包: 2. common-logging-xxx.jar 来自为知笔记(Wiz) 附件列表

  5. 使用Spring开发第一个HelloWorld应用

    http://www.importnew.com/13246.html 让我们用Spring来写第一个应用程序吧. 完成这一章要求: 熟悉Java语言 设置好Spring的环境 熟悉简单的Eclips ...

  6. 【Spring开发】—— Spring Core

    原文:[Spring开发]-- Spring Core 前言 最近由于一些工作的需要,还有自己知识的匮乏再次翻开spring.正好整理了一下相关的知识,弥补了之前对spring的一些错误认知.这一次学 ...

  7. Spring(一):eclipse上安装spring开发插件&下载Spring开发包

    eclipse上安装spring开发插件 1)下载安装插件包:https://spring.io/tools/sts/all 由于我的eclipse版本是mars 4.5.2,因此我这里下载的插件包是 ...

  8. Spring开发环境搭建教程

    Spring开发环境搭建 JDK7以上版本 eclispe for j2ee 4.0以上版本 Spring frameWorks 3.0以上版本 至于前两个我们就不介绍,直接百度就可以了,对于Spri ...

  9. Java WebService 教程系列之 Spring 整合 CXF

    Java WebService 教程系列之 Spring 整合 CXF 一.引入 jar 包 <dependency> <groupId>org.apache.cxf</ ...

随机推荐

  1. IHTMLDocument2 TO IWebBrowser2

    if(NULL != pIHTMLDocument2) { IHTMLWindow2* pIHTMLWindow2 = NULL; hr = pIHTMLDocument2->get_paren ...

  2. GitHub:Git的使用

    1.下载安装后设置姓名和邮箱地址 $ git config --global user.name "yourGithubName" $ git config --global us ...

  3. 拒绝了对对象 'sp_OACreate' (数据库 'mssqlsystemresource',架构 'sys')的 EXECUTE 权限。

    执行一个存储过程, 由于里面使用到了一些 --创建对象  EXEC sp_OACreate 'VBScript.RegExp', @objRegex OUT  --设置属性  EXEC sp_OASe ...

  4. hdu 5818 Joint Stacks (优先队列)

    Joint Stacks Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. 转载-lvs官方文档-Linux服务器集群系统(二)

    Linux服务器集群系统(二) LVS集群的体系结构 章文嵩 (wensong@linux-vs.org) 2002 年 4 月 本文主要介绍了LVS集群的体系结构.先给出LVS集群的通用体系结构,并 ...

  6. Http权威指南(cookie以及web认证机制)

    其实对于cookie,想必大家都不陌生,cookie目前主要用于客户端的识别技术. 说到客户端识别技术,就不得不说一个登录态的问题了.登录态顾名思义,用于验证用户的登录与否. 1.登录态 对于PC端网 ...

  7. Java基础面试题 (一)

    1.面向对象的三个特征 封装,继承,多态.这个应该是人人皆知,有时候也会加上抽象. 2.多态的好处 允许不同类对象对同一消息做出响应,即同一消息可以根据发送对象的不同而采用多种不同的行为方式(发送消息 ...

  8. height 与 min-height 的继承

    min-height: inherit; 继承父元素的 min-height: 80px; 但,不能继承父元素的 height: 200px; height: inherit; 能继承父元素的: he ...

  9. java 多线程超详细总结——阿里大牛熬夜整理

    引 如果对什么是线程.什么是进程仍存有疑惑,请先Google之,因为这两个概念不在本文的范围之内. 用多线程只有一个目的,那就是更好的利用cpu的资源,因为所有的多线程代码都可以用单线程来实现.说这个 ...

  10. LNMP架构基础搭建

    LNMP架构+wordpress博客 环境: centos6.7 2.6.32-573.el6.x86_64 nginx-1.6.3 mysql-5.5.49 php-5.3.27 wordpress ...