webservice-之使用axis+spring开发

2024-03-30 21:58

本文主要是介绍webservice-之使用axis+spring开发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、环境配置 :在 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();

              }

       }

}

这篇关于webservice-之使用axis+spring开发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/862692

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依