Springboot集成Axis2——通过wsdl生成webService

  • 背景介绍
  • 下载Axis2
  • 使用wsdl2java工具执行代码生成
  • 服务端代码生成命令
  • 客户端代码生成命令
  • 生成服务端代码
  • 生成客户端代码


背景介绍

客户方需要通过WebService进行消息交互,并且文档中规定了wsdl格式。由于目前Springboot对cxf框架支持较好,并没对axis进行较好的集成,但是客户放所规定的wsdl又使用到了仅axis支持的rpc模式,因此不得不使用axis作为Webservice框架进行服务的服务端和客户端的搭建。由于涉及到业务环境,所以代码暂不提供,仅以此文作为搭建流程的记录。

下载Axis2

Axis2提供了wsdl2java的工具包,首先需要现在Axis2至本地目录(不用是项目目录)。官网下载地址,本人下载的版本为axis2-1.7.9。

下载完成后解压,进入bin目录会发现wsdl2java.sh (windows使用 wsdl2java.bat)

springboot 普通类调用service 为空 springboot调用wsdl_代码生成

使用wsdl2java工具执行代码生成

服务端代码生成命令

命令格式为:

wsdl2java.sh -uri [wsdl文件路径] -d adb -s -ss -sd -ssi -o [代码生成路径]

例如:

wsdl2java.sh -uri ~alistair/notify-response.wsdl -d adb -s -ss -sd -ssi -o ~alistair/Projects/ws/server/notify

客户端代码生成命令

命令格式为:

wsdl2java.sh -uri [wsdl文件路径] -d adb -o [代码生成路径]

例如:

wsdl2java.sh -uri ~alistair/notify_response.wsdl -d adb -o ~alistair/Projects/ws/client

生成服务端代码

使用wsdl2java生成服务端代码,生成结果如下:

springboot 普通类调用service 为空 springboot调用wsdl_代码生成_02


查看生成的resources/services.xml,该文件是服务的配置文件,可以看出ResultNotify为红色,是因为代码生成是大小写有无,将其改为小写resultNotify即可(目前是笔者遇到的这个问题,有可能只是个例)

springboot 普通类调用service 为空 springboot调用wsdl_java_03


代码生成完成后,对代码结构进行调整

springboot 普通类调用service 为空 springboot调用wsdl_代码生成_04


将生成的配置文件放置resouces下,注意文件夹的路径,其中servicesPath后面会用到

springboot 普通类调用service 为空 springboot调用wsdl_代码生成_05


修改pom,引入axis2相关包:

<dependency>
	  <groupId>org.apache.axis2</groupId>
	  <artifactId>axis2-transport-http</artifactId>
	  <version>1.7.7</version>
	</dependency>
	<dependency>
	  <groupId>org.apache.axis2</groupId>
	  <artifactId>axis2-transport-local</artifactId>
	  <version>1.7.7</version>
	</dependency>
	<dependency>
	  <groupId>org.apache.axis2</groupId>
	  <artifactId>axis2-adb</artifactId>
	  <version>1.7.7</version>
	</dependency>

修改ServiceSkeleton类,增加服务端的业务处理逻辑:

public class CSPResponseServiceSkeleton
    implements CSPResponseServiceSkeletonInterface {
    /**
     * Auto generated method signature
     *
     * @param resultNotify0
     * @return resultNotifyResponse1
     */
    @Override
    public ResultNotifyResponse resultNotify(ResultNotify resultNotify0) {
        ResultNotifyResponse resultNotifyResponse = new ResultNotifyResponse();

        CSPResult result = new CSPResult();
        result.setResult(0);

        String errorDescription = new String();
        errorDescription.setString("this is alistair.chow's web service");
        result.setErrorDescription(errorDescription);

        resultNotifyResponse.setResultNotifyReturn(result);

        return resultNotifyResponse;
    }
}

增加severlet配置:

@Configuration
public class AxisConfig {

    @Bean
    public ServletRegistrationBean axisServlet(){
        ServletRegistrationBean axisServlet = new ServletRegistrationBean();
        axisServlet.setServlet(new AxisServlet());
        axisServlet.addUrlMappings("/services/*");
        // 设置服务路径,主要用于读取生成的services.xml文件,注意这里的serviesPath为保持和resouse下文件名一致
        String path = this.getClass().getResource("/servicesPath").getPath();
        axisServlet.addInitParameter("axis2.repository.path", path);
        axisServlet.setLoadOnStartup(1);

        return axisServlet;
    }
}

执行run,查看服务是否能正常运行:

springboot 普通类调用service 为空 springboot调用wsdl_代码生成_06

生成客户端代码

使用wsdl2java生成客户端代码,生成结果如下:

springboot 普通类调用service 为空 springboot调用wsdl_服务端_07


调整代码位置,并新建一个客户端测试类ServiceClient,用户向服务端测试发送请求:

springboot 普通类调用service 为空 springboot调用wsdl_服务端_08


代码如下:

public class CSPResponseServiceClient {

    public static void main(String[] args) throws Exception{
        CSPResponseServiceStub stub = new CSPResponseServiceStub();
        CSPResponseServiceStub.ResultNotify resultNotify = new CSPResponseServiceStub.ResultNotify();
        org.apache.axis2.databinding.types.soapencoding.String cspId = new org.apache.axis2.databinding.types.soapencoding.String();
        cspId.setString("a");
        resultNotify.setCSPID(cspId);
        CSPResponseServiceStub.ResultNotifyResponse notifyResponse = stub.resultNotify(resultNotify);
        System.out.println(notifyResponse.getResultNotifyReturn().getResult());
        System.out.println(notifyResponse.getResultNotifyReturn().getErrorDescription());
    }
}

执行该main方法,可以看到为服务端处理后的消息:

springboot 普通类调用service 为空 springboot调用wsdl_服务端_09