WebService的實現(xiàn)與調(diào)用的幾種方案

原生的WebService的實現(xiàn)方式

1、創(chuàng)建一個jar工程

2、定義一個我們要發(fā)布的接口

package haiyang.yu.webservice;

import javax.jws.WebService;

/**
 * Created on 2018-04-16 8:31
 * <p>Title:  haiyang.yu.webservice</p>
 * <p>Description: </p>
 *
 * @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
 * @version 1.0
 */
@WebService
public interface TimeAlarmBell {
    /**
     * 問候當(dāng)前時間
     * @param name your name
     * @return results
     */
    String giveMeDate(String name);
}

3、實現(xiàn)接口將并實現(xiàn)相應(yīng)的業(yè)務(wù)

package haiyang.yu.webservice;

import javax.jws.WebService;
import java.util.Date;

/**
 * Created on 2018-04-16 8:33
 * <p>Title:  haiyang.yu.webservice</p>
 * <p>Description: </p>
 *
 * @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
 * @version 1.0
 */
@WebService(endpointInterface= "haiyang.yu.webservice.TimeAlarmBell",serviceName="showDate")//指定webservice所實現(xiàn)的接口以及服務(wù)名稱
public class TimeAlarmBellImpl implements TimeAlarmBell {
    @Override
    public String giveMeDate(String name) {
        return name.concat(",您好!現(xiàn)在是北京時間:").concat(new Date().toString());
    }
}

4、發(fā)布webservice

package haiyang.yu.webservice;

import javax.xml.ws.Endpoint;

/**
 * Created on 2018-04-16 8:36
 * <p>Title:  haiyang.yu.webservice</p>
 * <p>Description: </p>
 *
 * @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
 * @version 1.0
 */
public class TimeAlarmBellPublish {

    public static void main(String[] args) {
        TimeAlarmBell showDate = new TimeAlarmBellImpl();
        //調(diào)用Endpoint的publish方法發(fā)布Web Service
        Endpoint.publish("http://192.168.10.104:8085/showDate", showDate);
        System.out.println("Web Service發(fā)布成功!");
    }
}

5、webservice的調(diào)用


package haiyang.yu.webservice;

//import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created on 2018-04-16 8:48
 * <p>Title:  haiyang.yu.webservice</p>
 * <p>Description: </p>
 *
 * @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
 * @version 1.0
 */
public class TimeAlarmBellConnection {
    
    public static void main(String[] args) {
        
//        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(
//                1, new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(false).build());
//        scheduledThreadPoolExecutor.scheduleAtFixedRate(new Runnable() {
//            @Override
//            public void run() {
                //創(chuàng)建WSDL的URL,注意不是服務(wù)地址
                URL url = null;
                try {
                    url = new URL("http://192.168.10.104:8085/showDate?wsdl");
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }

                //創(chuàng)建服務(wù)名稱
                //1.namespaceURI - 命名空間地址
                //2.localPart - 服務(wù)視圖名
                QName qname = new QName("http://webservice.yu.haiyang/", "showDate");

                //創(chuàng)建服務(wù)視圖
                //參數(shù)解釋:
                //1.wsdlDocumentLocation - wsdl地址
                //2.serviceName - 服務(wù)名稱
                Service service = Service.create(url, qname);
                //獲取服務(wù)實現(xiàn)類
                TimeAlarmBell showDate = service.getPort(TimeAlarmBell.class);
                //調(diào)用查詢方法
                String result = showDate.giveMeDate("KevinBruce");
                System.out.println(result);
//            }
//        }, 1, 2, TimeUnit.SECONDS);
        
    }
    
}

注釋掉的內(nèi)容中使用定時任務(wù)去循環(huán)調(diào)用webservice的接口了,如果想要使用這個定時任務(wù),需要引入common-lang3的jar包

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>

使用axis1.4的實現(xiàn)方式

下載地址:http://archive.apache.org/dist/ws/axis/1_4/

下載axis-bin-1_4.zip 這個包

使用axis1實現(xiàn)webservice在構(gòu)建時可能會相對麻煩一些,但是構(gòu)建成功后,對于一些webservice的方法的實現(xiàn)就簡單了很多,可以大大的提高開發(fā)效率。

1、創(chuàng)建一個war工程(這是必須的,axis1使用war來發(fā)布接口)

2、創(chuàng)建包并定義一個接口(正常的接口,不需要注解)

package haiyang.yu.axis1;

/**
 * Created on 2018-04-16 16:37
 * <p>Title:  haiyang.yu.axis1</p>
 * <p>Description: </p>
 *
 * @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
 * @version 1.0
 */
public interface Hello {

    /**
     * 獲取當(dāng)前時間
     * @param user 獲取用戶
     * @return Result
     */
    String getLocalDate(String user);
}

3、給這個接口定義一個實現(xiàn)類(該實現(xiàn)類需要有@WebService注解)

package haiyang.yu.axis1;

import javax.jws.WebService;
import java.util.Date;

/**
 * Created on 2018-04-16 16:38
 * <p>Title:  haiyang.yu.axis1</p>
 * <p>Description: </p>
 *
 * @author <a href="mailto:991138518@qq.com">yuhaiyang</a>
 * @version 1.0
 */
@WebService
public class HelloImpl implements Hello {

    @Override
    public String getLocalDate(String user) {
        return user.concat(",您好! 當(dāng)前時間為:").concat(new Date().toString());
    }

}

當(dāng)你寫完實現(xiàn)類的時候,你的webservice的接口就已經(jīng)實現(xiàn)了。接下來的任務(wù)就是配置了。

4、配置要發(fā)布為webservice的方法

  • 修改wabapps/WEB-INF/web.xml文件(添加一個servlet映射)。
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <servlet>
    <servlet-name>AxisServlet</servlet-name>
    <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>AxisServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
</web-app>
  • 創(chuàng)建一個server-config.wsdd文件(在文件中添加以下內(nèi)容)
<?xml version="1.0" encoding="UTF-8"?>  
<deployment xmlns="http://xml.apache.org/axis/wsdd/"  
    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">  
    <globalConfiguration>  
        <parameter name="sendMultiRefs" value="true" />  
        <parameter name="disablePrettyXML" value="true" />  
        <parameter name="adminPassword" value="admin" />  
        <parameter name="dotNetSoapEncFix" value="true" />  
        <parameter name="enableNamespacePrefixOptimization" value="false" />  
        <parameter name="sendXMLDeclaration" value="true" />  
        <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl" />  
        <parameter name="sendXsiTypes" value="true" />  
        <requestFlow>  
            <handler type="java:org.apache.axis.handlers.JWSHandler">  
                <parameter name="scope" value="session" />  
            </handler>  
            <handler type="java:org.apache.axis.handlers.JWSHandler">  
                <parameter name="scope" value="request" />  
                <parameter name="extension" value=".jwr" />  
            </handler>  
        </requestFlow>  
    </globalConfiguration>  
    <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper" />  
    <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder" />  
    <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler" />  
    <transport name="http">  
        <requestFlow>  
            <handler type="URLMapper" />  
            <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler" />  
        </requestFlow>  
        <parameter name="qs:list"  
            value="org.apache.axis.transport.http.QSListHandler" />  
        <parameter name="qs:wsdl"  
            value="org.apache.axis.transport.http.QSWSDLHandler" />  
        <parameter name="qs.list"  
            value="org.apache.axis.transport.http.QSListHandler" />  
        <parameter name="qs.method"  
            value="org.apache.axis.transport.http.QSMethodHandler" />  
        <parameter name="qs:method"  
            value="org.apache.axis.transport.http.QSMethodHandler" />  
        <parameter name="qs.wsdl"  
            value="org.apache.axis.transport.http.QSWSDLHandler" />  
    </transport>  
    <transport name="local">  
        <responseFlow>  
            <handler type="LocalResponder" />  
        </responseFlow>  
    </transport>  
  
    <!-- 想要添加或者取消修改一下內(nèi)容即可,AdminService與Version注銷不提供服務(wù)。 -->  
  
    <service name="SayHello" provider="java:RPC">  
        <parameter name="allowedMethods" value="*" />  
        <parameter name="className" value="haiyang.yu.axis1.HelloImpl" />  
        <namespace>http://www.yuhaiyang.com/ocean-axis/sayHello</namespace>  
    </service>  
</deployment>  

5、將工程打成war 包放到j(luò)etty或者是Tomcat的webapps下,同時將axis-bin-1_4.zip包中解壓出來的文件中的webapps目錄下的內(nèi)容復(fù)制到j(luò)etty或者是Tomcat的webapps下。

6、啟動容器我們定義的url即可。

使用axis2-1.7.7的實現(xiàn)方式

下載地址:http://axis.apache.org/axis2/java/core/download.html

下載 axis2-1.7.7-war.zip 這個包

沒使用過,后續(xù)更新

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評論 6 342
  • WebService介紹 首先我們來談一下為什么需要學(xué)習(xí)webService這樣的一個技術(shù)吧.... 問題一 如果...
    Java3y閱讀 9,775評論 5 139
  • axis全稱Apache Extensible Interaction System 即阿帕奇可擴展交互系統(tǒng)。Ax...
    Dews閱讀 830評論 0 2
  • 中國大地,變化日新月異。 洪洞縣剛建了新機場,從洪洞飛寧夏,機票優(yōu)惠的只有百十來塊錢。 洪武與金良是大學(xué)同學(xué),又同...
    蔣學(xué)明閱讀 963評論 23 46
  • 我和他的流年,至今整整有十年,就像Eason唱的“十年之前,我不認(rèn)識你,你不認(rèn)識我”,不過不像歌的傷感,我們不僅是...
    寶石花瓣閱讀 335評論 0 2

友情鏈接更多精彩內(nèi)容