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

相关文章

Spring Boot配置和使用两个数据源的实现步骤

《SpringBoot配置和使用两个数据源的实现步骤》本文详解SpringBoot配置双数据源方法,包含配置文件设置、Bean创建、事务管理器配置及@Qualifier注解使用,强调主数据源标记、代... 目录Spring Boot配置和使用两个数据源技术背景实现步骤1. 配置数据源信息2. 创建数据源Be

Spring Boot 3.x 中 WebClient 示例详解析

《SpringBoot3.x中WebClient示例详解析》SpringBoot3.x中WebClient是响应式HTTP客户端,替代RestTemplate,支持异步非阻塞请求,涵盖GET... 目录Spring Boot 3.x 中 WebClient 全面详解及示例1. WebClient 简介2.

Java中使用 @Builder 注解的简单示例

《Java中使用@Builder注解的简单示例》@Builder简化构建但存在复杂性,需配合其他注解,导致可变性、抽象类型处理难题,链式编程非最佳实践,适合长期对象,避免与@Data混用,改用@G... 目录一、案例二、不足之处大多数同学使用 @Builder 无非就是为了链式编程,然而 @Builder

在IntelliJ IDEA中高效运行与调试Spring Boot项目的实战步骤

《在IntelliJIDEA中高效运行与调试SpringBoot项目的实战步骤》本章详解SpringBoot项目导入IntelliJIDEA的流程,教授运行与调试技巧,包括断点设置与变量查看,奠定... 目录引言:为良驹配上好鞍一、为何选择IntelliJ IDEA?二、实战:导入并运行你的第一个项目步骤1

在MySQL中实现冷热数据分离的方法及使用场景底层原理解析

《在MySQL中实现冷热数据分离的方法及使用场景底层原理解析》MySQL冷热数据分离通过分表/分区策略、数据归档和索引优化,将频繁访问的热数据与冷数据分开存储,提升查询效率并降低存储成本,适用于高并发... 目录实现冷热数据分离1. 分表策略2. 使用分区表3. 数据归档与迁移在mysql中实现冷热数据分

Spring Boot从main方法到内嵌Tomcat的全过程(自动化流程)

《SpringBoot从main方法到内嵌Tomcat的全过程(自动化流程)》SpringBoot启动始于main方法,创建SpringApplication实例,初始化上下文,准备环境,刷新容器并... 目录1. 入口:main方法2. SpringApplication初始化2.1 构造阶段3. 运行阶

mybatis-plus QueryWrapper中or,and的使用及说明

《mybatis-plusQueryWrapper中or,and的使用及说明》使用MyBatisPlusQueryWrapper时,因同时添加角色权限固定条件和多字段模糊查询导致数据异常展示,排查发... 目录QueryWrapper中or,and使用列表中还要同时模糊查询多个字段经过排查这就导致只要whe

Spring Boot3.0新特性全面解析与应用实战

《SpringBoot3.0新特性全面解析与应用实战》SpringBoot3.0作为Spring生态系统的一个重要里程碑,带来了众多令人兴奋的新特性和改进,本文将深入解析SpringBoot3.0的... 目录核心变化概览Java版本要求提升迁移至Jakarta EE重要新特性详解1. Native Ima

Java中的xxl-job调度器线程池工作机制

《Java中的xxl-job调度器线程池工作机制》xxl-job通过快慢线程池分离短时与长时任务,动态降级超时任务至慢池,结合异步触发和资源隔离机制,提升高频调度的性能与稳定性,支撑高并发场景下的可靠... 目录⚙️ 一、调度器线程池的核心设计 二、线程池的工作流程 三、线程池配置参数与优化 四、总结:线程

Spring Boot 与微服务入门实战详细总结

《SpringBoot与微服务入门实战详细总结》本文讲解SpringBoot框架的核心特性如快速构建、自动配置、零XML与微服务架构的定义、演进及优缺点,涵盖开发环境准备和HelloWorld实战... 目录一、Spring Boot 核心概述二、微服务架构详解1. 微服务的定义与演进2. 微服务的优缺点三