Axis2+Spring简单实践

目录结构:

Axis2Demo
  |--src
      |--Axis2Server
          |--TestServer.java
  |--resources
      |--appContext.xml
      |--springmvc.xml
  |--WebRoot  
      |--WEB-INF
          |--services(固定名)
              |--myservices(此名可随便)
                  |--META-INF(固定名)
                      |--services.xml(固定名)
          |--web.xml

各个文件内容:

TestServer.java

服务具体逻辑实现类

import org.springframework.stereotype.Component;

/**
 * @author yanzy
 * @description
 * @date 2018-03-14 13:46
 * @created by intelliJ IDEA
 */
@Component(value = "testServer")
public class TestServer {
    public String greek(String greekWord){
        System.out.print("hello World!");
        return greekWord;
    }
}

appContext.xml

web.xml中通过contextConfigLocation指定的spring配置文件





    

springmvc.xml

web.xml中通过


        SpringMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath*:resources/springmvc.xml
        
        1
    
    
        SpringMVC
        /rest/*

指定的springmvc配置文件




    
    
        
        
    

services.xml

将pojo发布为webservice的关键文件,此处配置为集成spring框架的配置



    
    
        
            axis2 example
        
        
        
            org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
        
        
        testServer
        
        
            
        
       
        
    

web.xml

web项目入口,配置Context内容与servlet、filter,特别注意axis2配置中拦截的地址不要使用axis2/*否则会出现异常



BlazeDS
  BlazeDS Application
  
    contextConfigLocation
    classpath*:resources/appContext.xml
  

    
        
            org.springframework.web.context.ContextLoaderListener
        
  

 
        SpringMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath*:resources/springmvc.xml
        
        1
    
    
        SpringMVC
        /rest/*
    
 

    
        AxisServlet
        org.apache.axis2.transport.http.AxisServlet
        1
    
    
        AxisServlet
        /services/*
    
    

因为本项目选用maven作为构建端,故将pom文件中关于axis2的内容也贴出来,若有jar包冲突请自行解决

1.6.2

        
            org.apache.axis2
            axis2
            ${axis2.version}
        
        
            org.apache.axis2
            axis2-spring
            ${axis2.version}
        
        
            org.apache.axis2
            axis2-adb
            ${axis2.version}
        
        
            org.apache.axis2
            axis2-kernel
            ${axis2.version}
        
        
            org.apache.axis2
            axis2-transport-http
            ${axis2.version}
        
        
            org.apache.axis2
            axis2-transport-local
            ${axis2.version}
        
        
            org.apache.axis2
            org.apache.axis2.osgi
            ${axis2.version}
        
        
            org.apache.axis2
            axis2-jaxws
            ${axis2.version}
        

至此,项目就可以部署到tomcat中观看效果了

打开浏览器,浏览如下地址:http://localhost:8080/TYDGPServer/services/mySoapService?wsdl

可以看到webservice的描述文档内容:


Axis2+Spring简单实践_第1张图片
webservice信息描述文档

使用soapUI访问我们发出的webservice如下:


Axis2+Spring简单实践_第2张图片
soapUI访问webservice

大功告成!!

ps:java调用webservice

public static void main(String[] args) throws Exception
    {
        //  使用RPC方式调用WebService
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        //  指定调用WebService的URL
        EndpointReference targetEPR = new EndpointReference(
                "http://localhost:8080/axis2/services/SimpleService");
        options.setTo(targetEPR);
        //  指定getGreeting方法的参数值
        Object[] opAddEntryArgs = new Object[] {"超人"};
        //  指定getGreeting方法返回值的数据类型的Class对象
        Class[] classes = new Class[] {String.class};
        //  指定要调用的getGreeting方法及WSDL文件的命名空间
        QName opAddEntry = new QName("http://ws.apache.org/axis2", "getGreeting");
        //  调用getGreeting方法并输出该方法的返回值
        System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
        //  下面是调用getPrice方法的代码,这些代码与调用getGreeting方法的代码类似
        classes = new Class[] {int.class};
        opAddEntry = new QName("http://ws.apache.org/axis2", "getPrice");
        System.out.println(serviceClient.invokeBlocking(opAddEntry, new Object[]{}, classes)[0]);
    }

你可能感兴趣的:(Axis2+Spring简单实践)