zoukankan      html  css  js  c++  java
  • Axis发布Webservice服务

    一、服务端代码

    1、创建Maven工程

    注意pom.xml文件的配置,需要引入axis的相关包

         <dependencies>
                <!-- axis 1.4 jar start -->
                <dependency>
                    <groupId>org.apache.axis</groupId>
                    <artifactId>axis</artifactId>
                    <version>1.4</version>
                </dependency>
                <!-- 日志引入  -->
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                    <version>1.7.12</version>
                </dependency>
                <dependency>
                    <groupId>commons-discovery</groupId>
                    <artifactId>commons-discovery</artifactId>
                    <version>0.2</version>
                    <exclusions>
                        <exclusion>
                            <groupId>commons-logging</groupId>
                            <artifactId>commons-logging</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <dependency>
                    <groupId>org.apache.axis</groupId>
                    <artifactId>axis-jaxrpc</artifactId>
                    <version>1.4</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.axis</groupId>
                    <artifactId>axis-saaj</artifactId>
                    <version>1.4</version>
                </dependency>
                <dependency>
                    <groupId>wsdl4j</groupId>
                    <artifactId>wsdl4j</artifactId>
                    <version>1.4</version>
                </dependency>
                <!-- axis 1.4 jar end -->
            </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                        <showWarnings>true</showWarnings>
                    </configuration>
                </plugin>
                <!-- 运行tomcat7方法:tomcat7:run -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <!-- 指定端口 -->
                        <port>8080</port>
                        <!-- 请求路径 -->
                        <path>/</path>
                    </configuration>
                </plugin>
            </plugins>
        </build>

    2、在web.xml中配置axis的servlet

      <!-- axis 配置 -->
      <servlet>
        <servlet-name>axis</servlet-name>
        <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>axis</servlet-name>
        <url-pattern>/services/*</url-pattern>
      </servlet-mapping>

     3、写一个对外发布的接口

    /**
     * @author WGR
     * @create 2020/3/9 -- 20:20
     */
    public interface HelloService {
    
        public String sayHello(String info);
    }
    /**
     * @author WGR
     * @create 2020/3/9 -- 20:22
     */
    public class HelloServiceImpl implements HelloService {
        @Override
        public String sayHello(String info) {
            return "sayHello:"+info;
        }
    }

    4.通过server-config.wsdd文件对外发布服务

    server-config.wsdd文件存放在工程的WEB-INFO目录下(与web.xml同级目录,这个文件axis框架底层会去解析的,不用操心怎么去加载)

    <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
    
        <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
        <service name="HelloServiceImpl" provider="java:RPC">
            <parameter name="className" value="com.topcheer.axis.service.impl.HelloServiceImpl"/>
            <parameter name="allowedMethods" value="*"/>
        </service>
        <transport name="http">
            <requestFlow>
                <handler type="URLMapper"/>
            </requestFlow>
        </transport>
    </deployment>

    5、验证

    二、客户端代码

    1、仍然基于axis,建立HelloClient.java类:

    /**
     * @author WGR
     * @create 2020/3/9 -- 20:46
     */
    public class HelloClient {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Service service = new Service();
            try {
                Call call = (Call) service.createCall();
                //设置地址
                call.setTargetEndpointAddress("http://localhost:8080/services/HelloServiceImpl?wsdl");
                //设置要执行的方法(以下两种方式都可以)
                call.setOperationName(new QName("http://impl.service.axis.topcheer.com", "sayHello"));
                //设置要传入参数,如果没有要传入的参数,则不要写这个(参数名、参数类型、ParameterMode)
                call.addParameter("info", org.apache.axis.Constants.XSD_STRING, javax.xml.rpc.ParameterMode.IN);
                //设置返回的类型
                call.setReturnType(org.apache.axis.Constants.XSD_STRING);
                //调用WebService服务
                String info = "你好!";
                String result = (String) call.invoke(new Object[]{info});
                System.out.println(result);
            } catch (ServiceException e) {
                e.printStackTrace();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    
    }

     

  • 相关阅读:
    Java Servlet API中文说明文档
    JavaWeb学习总结(一)——JavaWeb开发入门
    Android ListView滑动底部自动加载更多
    Android TextView 手动上下滑动
    Android抽象布局——include、merge 、ViewStub
    Android 信鸽推送通知栏不显示推送的通知
    Twitter License for Android
    Uber License for Android
    Android Studio之build.gradle小技巧
    Android动画之硬件加速
  • 原文地址:https://www.cnblogs.com/dalianpai/p/12451590.html
Copyright © 2011-2022 走看看