美文网首页我爱编程
Axis2创建WebService实例 --- 接口发布

Axis2创建WebService实例 --- 接口发布

作者: 清风徐来水波不清 | 来源:发表于2018-05-19 16:58 被阅读0次

最近项目需要使用Axis2方式对外提供接口,项目是maven,在网上费尽心思的找了好久,相关maven axis2 项目搭建的文章都是前篇一律,最重要的是没有一个文章测试成功,不吐槽了,赶紧来一起来看看怎么使用Axis2创建WebService实例

开发环境

  • JDK1.8
  • Maven 3.6
  • IDEA

准备工作

  1. http://ws.apache.org/axis2/ 下载Axis2的最新版本

    image.png
  2. 将axis2-1.7.7-war.zip文件解压,解压其中axis2.war,其目录如下


    image.png

项目搭建

  1. 新建Mvaen工程


    image.png
  2. 在.pom文件中加入如下依赖,打包方式为war
<properties>
        <axis2.version>1.6.2</axis2.version>

    </properties>

<dependencies>

    <!--axis2 begin-->

    <dependency>

        <groupId>org.apache.axis2</groupId>

        <artifactId>axis2</artifactId>

        <version>${axis2.version}</version>

    </dependency>

    <dependency>

        <groupId>org.apache.axis2</groupId>

        <artifactId>axis2-spring</artifactId>

        <version>${axis2.version}</version>

    </dependency>

    <dependency>

        <groupId>org.apache.axis2</groupId>

        <artifactId>axis2-transport-http</artifactId>

        <version>${axis2.version}</version>

    </dependency>

    <dependency>

        <groupId>org.apache.axis2</groupId>

        <artifactId>axis2-transport-local</artifactId>

        <version>${axis2.version}</version>

    </dependency>

    <dependency>

        <groupId>org.apache.axis2</groupId>

        <artifactId>axis2-xmlbeans</artifactId>

        <version>${axis2.version}</version>

    </dependency>

    <!--axis2 end-->
</dependencies>

  1. 将之前解压的axis2.war目录下WEB-INF中的除了classes 文件夹,其他都拷贝到Maven项目的WEB-INF目录下


    image.png

    操作完后的maven目录结构如下


    image.png
  2. 编写webService
    IHelloServiceImpl.java 内容如下:
public class IHelloServiceImpl implements IHelloService {
   public String sayHello(String name) {
       return "Hello," + name;
   }
}
  1. 对webService进行配置,配置文件WEB-INF/services/hello/META-INF/services.xml


    image.png
<?xml version="1.0" encoding="UTF-8"?>
<!-- serviceGroup 中可以添加多个service  -->
<serviceGroup>
    <!-- 单独的一个service -->
<service name="hello" targetNamespace="http://ws.axis2Example/" >
    <!-- service 描述 -->
    <description>JNLPGenerator service</description>
    <!-- schemaNamespace 在调用的时候需要使用 -->
    <schema schemaNamespace="http://ws.axis2Example/" />
    <!-- 自己service 的实现类 -->
    <parameter name="ServiceClass" locked="false">com.test.axis2.IHelloServiceImpl</parameter>
    <!-- 接口中对外提供的方法-->
    <operation name="sayHello">
        <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </operation>
</service>
</serviceGroup>

如果实际使用web.xml 可以根据需要更改,这里使用官方war包里面的web.xml

测试

  1. 将刚才axis2.war文件拷贝到tomcat的webapps 目录下,或者使用的IDEA直接在启动tomecat的时候进行配置。下面是IDEA下将axis2.war 放在tomcat的webapps目录步骤。 打开tomcat配置,进入Deployment 选项,在+号出添加外部war文件,找到磁盘上axis2.war的位置,点击Apply完成配置,启动Tomcat 。


    image.png
  2. 访问http://localhost:8084/axis2/ (我的tomcat端口是8084),可以看到axis2首页,你已经成功了一半了。

    image.png
  3. 访问http://localhost:8084/WS/services/hello?wsdl 看到如下的XML文件,基本就成功了

    image.png
  4. 在浏览器测试接口调用方法传递参数看看结果,输入http://localhost:8084/WS/services/hello/sayHello?name=---bobo---

    image.png

相关文章

网友评论

    本文标题:Axis2创建WebService实例 --- 接口发布

    本文链接:https://www.haomeiwen.com/subject/dfqadftx.html