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

相关文章

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Spring 依赖注入与循环依赖总结

《Spring依赖注入与循环依赖总结》这篇文章给大家介绍Spring依赖注入与循环依赖总结篇,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. Spring 三级缓存解决循环依赖1. 创建UserService原始对象2. 将原始对象包装成工

Java中如何正确的停掉线程

《Java中如何正确的停掉线程》Java通过interrupt()通知线程停止而非强制,确保线程自主处理中断,避免数据损坏,线程池的shutdown()等待任务完成,shutdownNow()强制中断... 目录为什么不强制停止为什么 Java 不提供强制停止线程的能力呢?如何用interrupt停止线程s

SpringBoot请求参数传递与接收示例详解

《SpringBoot请求参数传递与接收示例详解》本文给大家介绍SpringBoot请求参数传递与接收示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录I. 基础参数传递i.查询参数(Query Parameters)ii.路径参数(Path Va

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Java MCP 的鉴权深度解析

《JavaMCP的鉴权深度解析》文章介绍JavaMCP鉴权的实现方式,指出客户端可通过queryString、header或env传递鉴权信息,服务器端支持工具单独鉴权、过滤器集中鉴权及启动时鉴权... 目录一、MCP Client 侧(负责传递,比较简单)(1)常见的 mcpServers json 配置

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队