参考网址:https://my.oschina.net/zimingforever/blog/212492

项目需要,这两天系统要调一个webservice的服务,webservice的东西都扔了好几年了,怎么使用都忘得一干二净了。以前都是使用系统现成的框架掉一个方法就行了,现在几乎是从0开始一点一点搭建环境啊

由于只是调用服务,所以我这边只要实现一下spring环境下接入websevice就行了

第一中尝试的是使用spring ws的WebServiceTemplate

配置的方法如下:

        <bean id="xxxWebService" class="org.springframework.ws.client.core.WebServiceTemplate">
            <property name="defaultUri" value="http://XXX.net:8080"/>        </bean>

第二种尝试我用了jaxws

配置方法如下:

    <bean id="xxxWebService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">        <property name="serviceInterface" value="com.xxx.UploadFileService"/>        <property name="namespaceUri" value="http://XXX.net:8080"/>        <property name="wsdlDocumentUrl" value="http://XXX.net:8080/XXXServicePort?WSDL" />    </bean>


第3中我尝试的是CXF

  <import resource="classpath:META-INF/cxf/cxf.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
     
    <jaxws:client id="xxxWebService" serviceClass="com.xxx.UploadFileService"   
            address="http://XXX.net:8080">  
    </jaxws:client>

第4种我用的是xfire,也是最后项目采用的方法

  <bean id ="dwdsspWebService" class ="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">        <property name ="serviceClass">            <value>com.xxx.UploadFileService</value>        </property>        <property name ="wsdlDocumentUrl">            <value>http://XXX.net:8080/XXXServicePort?WSDL</value>        </property>    </bean>


参考文档:http://blog.csdn.net/kkdelta/article/details/3987591

http://blog.csdn.net/kkdelta/article/details/7290769

http://blog.csdn.net/jadyer/article/details/9002984

http://blog.csdn.net/vickychen89/article/details/6606571

http://coach.iteye.com/blog/894159


第二种spring集成jaxws服务端

基于Spring IoC容器发布Web服务,能够大大降低WebService实现过程,也能够更好的与企业级应用进行整合,本文將和大家介绍如何基于Spring和JAX-WS发布WebService。

我们首先需要获取项目所依赖的Jar包,这个过程比较繁琐,笔者采用Maven构建项目,使用Maven进行项目管理的好处是我们只需要在pom.xml文件中配置依赖项目坐标,Maven就会自动將所需要的Jar包下载到本地仓库。

1.新建一个Maven Web项目,在pom.xml中添加如下内容:

        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>1.1.6</version>
        </dependency>123456789101112131415123456789101112131415

2.新建Web服务接口和实现类,这个过程和前面两篇文中相同。 
HelloWorld.Java

package com.csdn.ws.recipe03;import javax.jws.WebMethod;import javax.jws.WebService;@WebServicepublic interface HelloWorld {
    @WebMethod
    public String sayHello(String name);
}1234567891012345678910

HelloWorldImpl.java

package com.csdn.ws.recipe03;import javax.jws.WebService;import org.springframework.stereotype.Component;@Component@WebService(serviceName = "HelloWorldService", endpointInterface = "com.csdn.ws.recipe03.HelloWorld")public class HelloWorldImpl implements HelloWorld {

    public String sayHello(String name) {        return "Hello," + name;
    }
}12345678910111213141234567891011121314

不同的是在实现类中添加了注解@Component,该注解用于Spring查找组件。

3.在web.xml文件中添加spring的监听器配置:

<!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>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener></web-app>1234567891011121312345678910111213

4.新建一个source folder,名为config,在config下新建beans.xml,用于spring bean的配置。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.csdn.ws.recipe03"></context:component-scan>
    <bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
        <property name="baseAddress" value="http://localhost:8089/services/"/>
    </bean></beans>12345678910111213141234567891011121314

context:component-scan标签指定了查找组件的包名,SimpleJaxWsServiceExporter类的baseAddress属性用于指定webservice的根路径,完整的web服务地址=根路径+WebService名称。 
5.完整的项目结构如下图所示:

spring整合webservice方法_spring webservice

6.通过地址http://localhost:8089/services/HelloWorldService?wsdl查看WSDL文档。 
spring整合webservice方法_spring webservice_02 
说明WebService发布成功。

第五种是axis2+spring集成

1、新建一个web project项目,最终工程目录如下:

spring整合webservice方法_spring webservice_03

注意:本文只注重webservice服务器端的开发,因此com.ljq.client和com.ljq.test忽略不计

         

2、添加所需jar

spring整合webservice方法_spring webservice_04

            

3、接口HelloWorld


package com.ljq.service;

public interface HelloWorld {
   
public String greeting(String name);
   
public String print();
}

         

4、接口实现类HelloWorldBean

spring整合webservice方法_spring webservice_05


package com.ljq.service;

public class HelloWorldBean implements HelloWorld {
   
public String greeting(String name) {
       
return "你好 "+name;
   }

   
public String print() {
       
return "我叫林计钦";
   }
}

spring整合webservice方法_spring webservice_05

          

5、webservice类HelloWorldWebService

spring整合webservice方法_spring webservice_05


package com.ljq.service;

import org.apache.axis2.AxisFault;
import org.apache.axis2.ServiceObjectSupplier;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.description.Parameter;
import org.apache.axis2.i18n.Messages;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
* 可能出现Axis2 spring bean not found 或者 Spring applicationContext not found。
*
* 解决办法:构建自己的ServiceObjectSupplier,实现接口ServiceObjectSupplier,同时也实现Spring的ApplicationContextAware接口
*
*
*
@author Administrator
*
*/
public class HelloWorldWebService implements ServiceObjectSupplier,
       ApplicationContextAware {
   
private static ApplicationContext ctx;

   
public Object getServiceObject(AxisService axisService) throws AxisFault {
       Parameter springBeanName
= axisService.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"));
       }

   }

   
public void setApplicationContext(ApplicationContext ctx)
           
throws BeansException {
       
this.ctx = ctx;
   }

}

spring整合webservice方法_spring webservice_05

            

6、配置web.xml文件

spring整合webservice方法_spring webservice_05


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee
   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
   
<!-- 添加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>

spring整合webservice方法_spring webservice_05

       

7、在WEB-INF目录下配置applicationContext.xml(不存在则自己创建)

spring整合webservice方法_spring webservice_05


<?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="applicationContext"
       class
="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
   
<bean id="helloWorld" class="com.ljq.service.HelloWorldBean"></bean>
</beans>

spring整合webservice方法_spring webservice_05

         

8、在WEB-INF\services\axis\META-INF\目录下配置services.xml(不存在则自己创建)

spring整合webservice方法_spring webservice_05


<?xml version="1.0" encoding="UTF-8"?>
<service name="hwWebService">
   
<description>axis2与spring集成案例</description>
   
<!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 -->
   
<parameter name="ServiceObjectSupplier">
       org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
   
</parameter>
   
<!--
      SpringBeanName固定的不能改
      helloWorld是spring中注册的实现类得id
   
-->
   
<parameter name="SpringBeanName">helloWorld</parameter>
   
<!--
   在这里最值得注意的是<messageReceivers>元素,该元素用于设置处理WebService方法的处理器。
   例如,getGreeting方法有一个返回值,因此,需要使用可处理输入输出的RPCMessageReceiver类,
   而update方法没有返回值,因此,需要使用只能处理输入的RPCInOnlyMessageReceiver类。
   
-->
   
<messageReceivers>
       
<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" />
   
</messageReceivers>
</service>