Web服務入門

    這一學期都在嘗試瞭解Web Service究竟是什麼。最近項目要求J2EE和.NET之間通訊,並且要把實現的跨平臺功能也便於往後擴展,總之平臺語言的均可能不同。WS給個人感受就像朦朧的仙女,怎麼也抓不到。今天特意寫此文章,走出工程實踐的第一步。java

    首先,給出維基百科對其的定義。瀏覽器

    Web服務是一種服務導向架構的技術,經過標準的Web協議提供服務,目的是保證不一樣平臺的應用服務能夠互操做。服務器

    根據W3C的定義,Web服務(Web service)應當是一個軟件系統,用以支持網絡間不一樣機器的互動操做。網絡服務一般是許多應用程序接口API)所組成的,它們透過網絡,例如國際互聯網(Internet)的遠程服務器端,執行客戶所提交服務的請求。網絡

    這兩段話對我來講,突出的兩個字是「服務」,服務這個東西好啊。爲何呢?舉個彷佛不恰當的例子,在使用Windows的時候,開機啓動項有不少服務,當咱們須要的時候打開,不須要的時候關閉。這有什麼好處呢?第一,很方便,由於能夠本身即時控制。第二,很節省,我須要你了就要你,不要你了就不要了,這讓我想起了軟件即服務,你不須要買個人硬件設備,你須要個服務器我給你開個得了,這不就是如今購買雲服務的作法嗎?我的感受最好的理解例子就是雲服務器。架構

    很少扯了,先給出一個小小的入門代碼。app

    

    

package com.liu;

import javax.jws.*;
import javax.jws.soap.*;
import javax.jws.soap.SOAPBinding.Style;

@WebService
@SOAPBinding(style=Style.RPC)
public interface TimeServer {
	@WebMethod String getTimeAsString();
	@WebMethod long getTimeAsElapsed();
}

////////////////
package com.liu;

import java.util.Date;

import javax.jws.WebService;


@WebService(endpointInterface="com.liu.TimeServer")
public class TimeServerImpl implements TimeServer {

	@Override
	public String getTimeAsString() {
		return new Date().toString();
	}

	@Override
	public long getTimeAsElapsed() {
		return new Date().getTime();
	}

}

////////////////
package com.liu;

import javax.xml.ws.Endpoint;

public class TimeServerPublisher {

	public static void main(String[] args) {
		Endpoint.publish("http://127.0.0.1:9876/liu", new TimeServerImpl());
	}
}

    打開命令行,進入文件目錄使用javac com/liu/*.java,獲得三個.class文件。而後使用java com.liu.TimeServerPublisher。若是代碼寫正確的話,會啥也不出現(QAQ)ide

    這就至關於服務發佈了,你們能夠打開瀏覽器請求這個服務。輸入地址http://localhost:9876/liu?wsdlsvn

爲何要寫wsdl呢?我感受就是告訴他,我要你的wsdl哦。微服務

    貼出個人工具

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
 Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<!--
 Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e. 
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://liu.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://liu.com/" name="TimeServerImplService">
<types/>
<message name="getTimeAsString"/>
<message name="getTimeAsStringResponse">
<part name="return" type="xsd:string"/>
</message>
<message name="getTimeAsElapsed"/>
<message name="getTimeAsElapsedResponse">
<part name="return" type="xsd:long"/>
</message>
<portType name="TimeServer">
<operation name="getTimeAsString">
<input wsam:Action="http://liu.com/TimeServer/getTimeAsStringRequest" message="tns:getTimeAsString"/>
<output wsam:Action="http://liu.com/TimeServer/getTimeAsStringResponse" message="tns:getTimeAsStringResponse"/>
</operation>
<operation name="getTimeAsElapsed">
<input wsam:Action="http://liu.com/TimeServer/getTimeAsElapsedRequest" message="tns:getTimeAsElapsed"/>
<output wsam:Action="http://liu.com/TimeServer/getTimeAsElapsedResponse" message="tns:getTimeAsElapsedResponse"/>
</operation>
</portType>
<binding name="TimeServerImplPortBinding" type="tns:TimeServer">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="getTimeAsString">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal" namespace="http://liu.com/"/>
</input>
<output>
<soap:body use="literal" namespace="http://liu.com/"/>
</output>
</operation>
<operation name="getTimeAsElapsed">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal" namespace="http://liu.com/"/>
</input>
<output>
<soap:body use="literal" namespace="http://liu.com/"/>
</output>
</operation>
</binding>
<service name="TimeServerImplService">
<port name="TimeServerImplPort" binding="tns:TimeServerImplPortBinding">
<soap:address location="http://localhost:9876/liu"/>
</port>
</service>
</definitions>

    這就是獲得的wsdl。這個是自動生成的,咱們可使用jdk自帶的工具wsimport把wsdl翻譯成java

    獲得TimeServer.java,TimeServerImplService.java

package com.liu;

import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.Action;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.9-b130926.1035
 * Generated source version: 2.2
 * 
 */
@WebService(name = "TimeServer", targetNamespace = "http://liu.com/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface TimeServer {


    /**
     * 
     * @return
     *     returns java.lang.String
     */
    @WebMethod
    @WebResult(partName = "return")
    @Action(input = "http://liu.com/TimeServer/getTimeAsStringRequest", output = "http://liu.com/TimeServer/getTimeAsStringResponse")
    public String getTimeAsString();

    /**
     * 
     * @return
     *     returns long
     */
    @WebMethod
    @WebResult(partName = "return")
    @Action(input = "http://liu.com/TimeServer/getTimeAsElapsedRequest", output = "http://liu.com/TimeServer/getTimeAsElapsedResponse")
    public long getTimeAsElapsed();

}

////////////////
package com.liu;

import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.9-b130926.1035
 * Generated source version: 2.2
 * 
 */
@WebServiceClient(name = "TimeServerImplService", targetNamespace = "http://liu.com/", wsdlLocation = "http://127.0.0.1:9876/liu?wsdl")
public class TimeServerImplService
    extends Service
{

    private final static URL TIMESERVERIMPLSERVICE_WSDL_LOCATION;
    private final static WebServiceException TIMESERVERIMPLSERVICE_EXCEPTION;
    private final static QName TIMESERVERIMPLSERVICE_QNAME = new QName("http://liu.com/", "TimeServerImplService");

    static {
        URL url = null;
        WebServiceException e = null;
        try {
            url = new URL("http://127.0.0.1:9876/liu?wsdl");
        } catch (MalformedURLException ex) {
            e = new WebServiceException(ex);
        }
        TIMESERVERIMPLSERVICE_WSDL_LOCATION = url;
        TIMESERVERIMPLSERVICE_EXCEPTION = e;
    }

    public TimeServerImplService() {
        super(__getWsdlLocation(), TIMESERVERIMPLSERVICE_QNAME);
    }

    public TimeServerImplService(WebServiceFeature... features) {
        super(__getWsdlLocation(), TIMESERVERIMPLSERVICE_QNAME, features);
    }

    public TimeServerImplService(URL wsdlLocation) {
        super(wsdlLocation, TIMESERVERIMPLSERVICE_QNAME);
    }

    public TimeServerImplService(URL wsdlLocation, WebServiceFeature... features) {
        super(wsdlLocation, TIMESERVERIMPLSERVICE_QNAME, features);
    }

    public TimeServerImplService(URL wsdlLocation, QName serviceName) {
        super(wsdlLocation, serviceName);
    }

    public TimeServerImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
        super(wsdlLocation, serviceName, features);
    }

    /**
     * 
     * @return
     *     returns TimeServer
     */
    @WebEndpoint(name = "TimeServerImplPort")
    public TimeServer getTimeServerImplPort() {
        return super.getPort(new QName("http://liu.com/", "TimeServerImplPort"), TimeServer.class);
    }

    /**
     * 
     * @param features
     *     A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.  Supported features not in the <code>features</code> parameter will have their default values.
     * @return
     *     returns TimeServer
     */
    @WebEndpoint(name = "TimeServerImplPort")
    public TimeServer getTimeServerImplPort(WebServiceFeature... features) {
        return super.getPort(new QName("http://liu.com/", "TimeServerImplPort"), TimeServer.class, features);
    }

    private static URL __getWsdlLocation() {
        if (TIMESERVERIMPLSERVICE_EXCEPTION!= null) {
            throw TIMESERVERIMPLSERVICE_EXCEPTION;
        }
        return TIMESERVERIMPLSERVICE_WSDL_LOCATION;
    }

}

    至此,一個簡單的示例完成了。我在思考究竟爲何須要將wsdl轉換成java。鄙人感受一種使用場景爲客戶端類型未知,當客戶端須要這個功能的時候,經過wsdl將其傳遞到客戶端,而後變成客戶端的功能。

    這兩天深刻理解WS以後還會寫一篇SOAP,SOA,微服務,SaaS等一個關於服務的體系結構。

相關文章
相關標籤/搜索