作者: qukmei
保留原作的出处:http://www.iblog.com/index.php?op=ViewArticle&articleId=28581&blogId=1499
eclipse resin axis1.4 spring
spring在整合axis上还是很不错的。
1. 环境配置
1、 基本设置,略
2、 在eclipse中配置引入相应的Spring框架、axis包,略。
因为axis还有一些可选包,所以可以把一些可选包都引进来,虽然网上下载的axis1.4只有核心包,但通过happyaxis.jsp,可以把相关可选包都找到。
2. 场景描述
在本例中,我们要完成的是通过Web Service调用到Spring工程中的getMessage的方法,传入人名,然后返回相应的Say Hello to somebody的字符串,并将调用后的字符串打印到前台Application界面中。
3. 代码开发
在MyEclipse中建立一个新的J2EE的Web Project。
编写IHelloWorld接口文件,代码如下:
package com.test.www;
/**
* Spring工程中要使用的接口文件
*/
public interface IHelloWorld {
public String getMessage(String name);
}
编写HelloWorldImpl实现文件,代码如下:
package com.test.www.impl;
import com.test.www.IHelloWorld;
/**
* Spring工程中要使用的接口文件
*/
public class HelloWorldImpl implements IHelloWorld{
private String helloStr; //Spring中需要注入的字符串
/**
* 实现接口中的方法,得到Say Hello to <somebody>的字符串
*/
public String getMessage(String name){
return helloStr+":"+name;
}
public String getHelloStr() {
return helloStr;
}
public void setHelloStr(String helloStr) {
this.helloStr = helloStr;
}
}
编写Web-INF下的Web工程文件Web.xml,具体配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!—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发布路径 -->
</web-app>
简要说明:这个配置文件里,可能还会有许多,不要怕,只需要把关于axis的copy到你自己的web.xml就可以了,不会冲突的,当然也可以稍加修改的,比如:/services/*.jws,这样看起来标准一些,其实这都是可以调试修改的。
编写Web-INF下的Spring工程文件xxx-services.xml,具体配置如下:
<?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="helloWorld"
class="com.test.www.impl.HelloWorldImpl">
<property name="helloStr">
<value>Say Hello to :</value>
</property>
</bean>
</beans>
原文在这里有一些小错,不过是可以看出来的,我把正确的贴上来了,上面这段要放到services的配置下,记得要把包名和类名写对。
以上就完成了整个Spring的代码部分的编写,好像到了这个地方还一直没有介绍Web Service的部分(除了如何配置axis)。其实在Spring中对Web Service进行封装很简单,仅仅需要继承org.springframework.remoting.jaxrpc.ServletEndpointSupport类,实现里面的一些方法,包装一次,将其发布出来就可以勒。
编写包装成Web Service的JaxRpcHelloWorld类,代码如下:
package com.test.www.webservice;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
import com.test.www.IHelloWorld;
public class JaxRpcHelloWorld extends ServletEndpointSupport implements IHelloWorld{
private IHelloWorld helloWorld;
protected void onInit() throws ServiceException {
//在Spring容器中获取Bean的实例
helloWorld = (IHelloWorld) getApplicationContext().getBean(
"helloWorld");
}
public String getMessage(String name) throws RemoteException {
//执行Bean中的相同的方法
return helloWorld.getMessage(name);
}
}
最后编写server-config.wsdd文件,发布Web Service,具体代码如下:
<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="HelloWorld" provider="java:RPC">
<parameter name="className"
value="com.test.www.webservice.JaxRpcHelloWorld"/>
<parameter name="allowedMethods" value="*"/>
</service>
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
</requestFlow>
</transport>
</deployment>
所有的工作全部完成,接下来只需要启动resin来验证你的Web Service是否已经发布成功,启动resin后,在你的浏览器中输入:http://localhost:8080/<YourProject>/services/HelloWorld?wsdl,如果能发现HelloWorld(wdsl)等信息,恭喜你,你的所有的工作都已经完成。
4. 测试调用
编写一个叫TestWebServiceClient类,代码如下:
import java.net.URL;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.client.ServiceFactory;
public class testWebServiceClient {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String wsdlUrl = "http://localhost:8080/services/HelloWorld?wsdl";
String nameSpaceUri = "http://localhost:8080/services/HelloWorld";
// 创建调用对象
Service service = new Service();
Call call = null;
call = (Call) service.createCall();
// 调用sayHello
System.out.println(">>>getMessage");
call.setOperationName(new QName(nameSpaceUri, "getMessage"));
call.setTargetEndpointAddress(new java.net.url(http://www.zhmy.com/wsdlUrl));
String ret = (String) call.invoke(new Object[] { "ABC" });
System.out.println("return value is " + ret);
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行出来的结果,可想而知,会在控制台上输出一排字,内容如下:
Say Hello to:ABC
5:总结分析
因为用的是resin,所以要一定要配相关发布路径,要不然会报i18n的错的。
tomcat下更好弄。
这样就把resin+spring+axis结合起来了,与自己目前相关应用结合,就可以做实际业务处理了。
分享到:
相关推荐
struts2 spring hibernate ajax 整合例子
开发基于Struts Spring Hibernate Ajax整合配置文件的配置介绍。
《疯狂Ajax讲义:Prototype/jQuery+DWR+Spring+Hibernate整合开发》是《基于J2EE的Ajax宝典》的第二版。《基于J2EE的Ajax宝典》面市近2年,作为Ajax领域最全面、实用的图书,一直深受读者的好评。全书主要分为三个...
Spring + Hibernate + Spring mvc +Ajax 整合 框架,一个简单的添加删除。
整合的四大框架项目 spring hibernate struts ajax整合项目源代码 spring hibernate struts ajax整合项目源代码
1、运用了struts2 spring hibernate 各种技术 2、ajax 技术异步刷新
一个spring和AJAX整合的demo,下载下来就可以在这个demo的基础上开发自己的功能,节省自己搭框架的时间l..000000000000000
struts+hiernate+Spring+ajax(四)整合
spring security 安全认证与资源管理,,安全性特别高,,可以配置更多的安全设置。在许多企业应用中,都选用了这项技术。该代码主要将ajax请求与from请求区别。供大家参考!
三期答辩课程设计,人事管理系统Mybatis+Spring+Spring MVC框架整合AJAX+JQuery实现登录、注册以及验证码和验证注册用户信息,包括数据库备份,课设说明,答辩PPT,Windows下sts(eclipse) 。
Spring Boot和ajax实现信息查询页面,项目使用idea搭建。适合学习spring boot,前后端数据连接的朋友使用。
参照jpetstore做出的Struts1.1+spring2.5+ibatis2.3+Ajax的整合、从页面输入学生的Id、光标离开后、页面利用无刷新技术从数据库取出显示在页面上。
《疯狂Ajax讲义:Prototype/jQuery+DWR+Spring+Hibernate整合开发》是《基于J2EE的Ajax宝典》的第二版。《基于J2EE的Ajax宝典》面市近2年,作为Ajax领域最全面、实用的图书,一直深受读者的好评。全书主要分为三个...
本人系统学习Struts2过程中的全部例子(推荐给初学者,能够系统的掌握Struts2的全部特性) 简单的例子,国际化,拦截器,类型转换,输入校验,OGNL,标签库,文件上传与下载,整合Spring 开发环境:Myeclipse7.0
JavaEE多层架构Struts2+Spring3+Hibernate3+Ajax的整合JavaEE多层架构Struts2+Spring3+Hibernate3+Ajax的整合JavaEE多层架构Struts2+Spring3+Hibernate3+Ajax的整合
struts2 spring2 hibernate3 ajax 整合的一个小项目
这是一个ajax+spring+hibernate整合project,里面有数据库创建语句。代码清晰,适用于初学者。
全面覆盖据ava web 开发基础JSP+Servlet+Ajax+ssh框架整合 提供大量案例进行分析
1.通过google ehcache-spring-annotatios.jar自动注解方式实现整合Spring+Ehcache。 2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。...
李刚的疯狂Ajax讲义——Prototype/jQuery+DWR+Spring+Hibernate整合开发(完整源代码)绝对完整版,一共分9个部分