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();
        }
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM