webservices系列(六)——spring整合Axis2

来源:互联网 发布:java pdf生成 编辑:程序博客网 时间:2024/06/05 12:42

1.新建一个动态网站项目webservice_test4,最终工程目录如下


2.准备这些jar


3.web.xml中配置springaxis

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>webservice_test4</display-name>  <!-- 添加spring监听器 -->  <listener>  <listener-class>  org.springframework.web.context.ContextLoaderListener  </listener-class>  </listener>  <!-- 加载spring配置文件 -->  <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>/WEB-INF/applicationContext.xml</param-value>  </context-param>    <!-- 注册Axis2的servlet -->  <servlet>  <servlet-name>AxisServlet</servlet-name>  <servlet-class>  org.apache.axis2.transport.http.AxisServlet  </servlet-class>  <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>  <servlet-name>AxisServlet</servlet-name>  <url-pattern>/services/*</url-pattern>  </servlet-mapping>    <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>
4.webservice实现代码

HelloWorld.java

public interface HelloWorld {public String greeting(String name);public String print();}HelloWorldImpl.javapublic class HelloWorldImpl implements HelloWorld{public String greeting(String name) {return "hello " + name;}public String print() {return "print";}}
HelloWorldService.java

public class HelloWorldService implements ServiceObjectSupplier, ApplicationContextAware{private ApplicationContext ctx;public void setApplicationContext(ApplicationContext arg0) throws BeansException {ctx = arg0;}@Overridepublic Object getServiceObject(AxisService arg0) throws AxisFault {Parameter springBeanName = arg0.getParameter("SpringBeanName");String beanName = ((String)springBeanName.getValue()).trim();if(beanName != null) {if(ctx == null) {throw new AxisFault("ApplicationContext is null");}if(ctx.getBean(beanName) == null) {throw new AxisFault("axis2 can't find Spring Bean :" + beanName);}return ctx.getBean(beanName);} else {throw new AxisFault(Messages.getMessage("paramIsNotSpecified", "SERVICE_SPRING_BEANNAME"));}}}
5.axis2的接口配置文件services.xml

<?xml version="1.0" encoding="UTF-8"?><service name="hwWebService"><description>axis2与spring集成</description><parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter><parameter name="SpringBeanName">helloWorld</parameter><messageReceiver><messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /></messageReceiver></service>
6.spring的配置文件applicationContext.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="applictationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" /><bean id="helloWorld" class="com.lc.service.HelloWorldImpl"></bean></beans>
发布到tomcat上,打开链接http://localhost:8080/webservice_test4/services/hwWebService?wsdl



参考资料

[1].spring和axis2整合:http://www.cnblogs.com/yuxuan/p/4028359.html

0 0
原创粉丝点击