加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice-之使用axis+spring开发

发布时间:2020-12-17 02:42:52 所属栏目:安全 来源:网络整理
导读:一、环境配置 :在 eclipse 中配置引入相应的 Spring 框架( core/Remoting/Web )、 axis 包。 ? 二、代码开发 1、 ? 在 MyEclipse 中建立一个新的 J2EE 的 Web Project, 新建 java 包 test 。 2、 ? 接口文件 HelloWorldRemote.java package test; //Sprin

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

????????????? }

?????? }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读