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等一个关于服务的体系结构。

相关文章
相关标签/搜索