Web service是一個平台獨立的,低耦合的,自包含的、基於可編程的web的應用程序,可使用開放的XML(標准通用標記語言下的一個子集)標准來描述、發布、發現、協調和配置這些應用程序,用於開發分布式的互操作的應用程序。Web Service技術, 能使得運行在不同機器上的不同應用無須借助附加的、專門的第三方軟件或硬件, 就可相互交換數據或集成。依據Web Service規范實施的應用之間, 無論它們所使用的語言、 平台或內部協議是什麼, 都可以相互交換數據。Web Service是自描述、 自包含的可用網絡模塊, 可以執行具體的業務功能。Web Service也很容易部署, 因為它們基於一些常規的產業標准以及已有的一些技術,諸如標准通用標記語言下的子集XML、HTTP。Web Service減少了應用接口的花費。Web Service為整個企業甚至多個組織之間的業務流程的集成提供了一個通用機制。
(詳情可參考webservice的百度百科)
Web Service平台需要一套協議來實現分布式應用程序的創建。任何平台都有它的數據表示方法和類型系統。要實現互操作性,Web Service平台必須提供一套標准的類型系統,用於溝通不同平台、編程語言和組件模型中的不同類型系統。這些協議有: XML和XSD 可擴展的標記語言(標准通用標記語言下的一個子集)是Web Service平台中表示數據的基本格式。除了易於建立和易於分析外,XML主要的優點在於它既與平台無關,又與廠商無關。XML是由萬維網協會(W3C)創建,W3C制定的XML SchemaXSD 定義了一套標准的數據類型,並給出了一種語言來擴展這套數據類型。 Web Service平台是用XSD來作為數據類型系統的。當你用某種語言如VB. NET或C# 來構造一個Web Service時,為了符合Web Service標准,所有你使用的數據類型都必須被轉換為XSD類型。如想讓它使用在不同平台和不同軟件的不同組織間傳遞,還需要用某種東西將它包裝起來。這種東西就是一種協議,如 SOAP。 SOAP SOAP即簡單對象訪問協議(Simple Object Access Protocol),它是用於交換XML(標准通用標記語言下的一個子集)編碼信息的輕量級協議。它有三個主要方面:XML-envelope為描述信息內容和如何處理內容定義了框架,將程序對象編碼成為XML對象的規則,執行遠程過程調用(RPC)的約定。SOAP可以運行在任何其他傳輸協議上。例如,你可以使用 SMTP,即因特網電子郵件協議來傳遞SOAP消息,這可是很有誘惑力的。在傳輸層之間的頭是不同的,但XML有效負載保持相同。 Web Service 希望實現不同的系統之間能夠用“軟件-軟件對話”的方式相互調用,打破了軟件應用、網站和各種設備之間的格格不入的狀態,實現“基於Web無縫集成”的目標。 WSDL Web Service描述語言WSDL 就是用機器能閱讀的方式提供的一個正式描述文檔而基於XML(標准通用標記語言下的一個子集)的語言,用於描述Web Service及其函數、參數和返回值。因為是基於XML的,所以WSDL既是機器可閱讀的,又是人可閱讀的。 UDDI UDDI 的目的是為電子商務建立標准;UDDI是一套基於Web的、分布式的、為Web Service提供的、信息注冊中心的實現標准規范,同時也包含一組使企業能將自身提供的Web Service注冊,以使別的企業能夠發現的訪問協議的實現標准。Web服務為Internet上應用程序之間的交互提供了方便
Web服務也減輕了企業級應用中出現的異構系統的整合危機
Web服務的優勢包括:

1.使用JAX-WS發布和調用web服務
(JAX-WS--->web服務標准,jdk中的一個組件,集成了JAXB,本質上其實是Scoket編程)
01.發布自己的ws服務

源碼介紹:
HelloService.java

package cn.myservice;
//Service端(服務器端)
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
//局域網任何人都可以訪問
@WebService
public class HelloService {
public void say(String name){
System.out.println("Hello"+name);
}
public static void main(String[] args) {
/**
* 端口號:50000
* 一個標識(區分的作用):hello
* 發布者:new HelloService()
*/
Endpoint.publish("http://localhost:50000/hello", new HelloService());
System.out.println("server is listening ....");
}
}
View Code
運行效果:

大家也可以用cmd命令 netstat -na來看看有沒有我們發布的端口號的存在(如下圖,它是處於監聽狀態的)

現在我們的局域網上都可以訪問我發布的(http://localhost:50000/hello)這個服務了
效果:

02.調用自己的ws服務
001.MyEclipse自帶工具調用
步驟一:

步驟二:

步驟三:

步驟四:

步驟五:

步驟六:

步驟七:

這時就完成了調用,控制台就會打印相應的信息

002.書寫代碼調用

其中我們myservice包中的類我們是不需要自己去寫的,我們可以使用jdk中的wsimport.exe利用我們cmd命令給我們生成(當然是在保證我們的jdk安裝,環境變量配置成功的情況下)
操作如下:

這時,我們來看看我們的c盤根目錄:

源碼介紹:
1.HelloService.java

package cn.myservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.Action;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.9-b130926.1035
* Generated source version: 2.2
*
*/
@WebService(name = "HelloService", targetNamespace = "http://myservice.cn/")
@XmlSeeAlso({
ObjectFactory.class
})
public interface HelloService {
/**
*
* @param arg0
*/
@WebMethod
@RequestWrapper(localName = "say", targetNamespace = "http://myservice.cn/", className = "cn.myservice.Say")
@ResponseWrapper(localName = "sayResponse", targetNamespace = "http://myservice.cn/", className = "cn.myservice.SayResponse")
@Action(input = "http://myservice.cn/HelloService/sayRequest", output = "http://myservice.cn/HelloService/sayResponse")
public void say(
@WebParam(name = "arg0", targetNamespace = "")
String arg0);
}
View Code
2.HelloServiceService.java

package cn.myservice;
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 = "HelloServiceService", targetNamespace = "http://myservice.cn/", wsdlLocation = "http://localhost:50000/hello?wsdl")
public class HelloServiceService
extends Service
{
private final static URL HELLOSERVICESERVICE_WSDL_LOCATION;
private final static WebServiceException HELLOSERVICESERVICE_EXCEPTION;
private final static QName HELLOSERVICESERVICE_QNAME = new QName("http://myservice.cn/", "HelloServiceService");
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("http://localhost:50000/hello?wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
HELLOSERVICESERVICE_WSDL_LOCATION = url;
HELLOSERVICESERVICE_EXCEPTION = e;
}
public HelloServiceService() {
super(__getWsdlLocation(), HELLOSERVICESERVICE_QNAME);
}
public HelloServiceService(WebServiceFeature... features) {
super(__getWsdlLocation(), HELLOSERVICESERVICE_QNAME, features);
}
public HelloServiceService(URL wsdlLocation) {
super(wsdlLocation, HELLOSERVICESERVICE_QNAME);
}
public HelloServiceService(URL wsdlLocation, WebServiceFeature... features) {
super(wsdlLocation, HELLOSERVICESERVICE_QNAME, features);
}
public HelloServiceService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public HelloServiceService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* @return
* returns HelloService
*/
@WebEndpoint(name = "HelloServicePort")
public HelloService getHelloServicePort() {
return super.getPort(new QName("http://myservice.cn/", "HelloServicePort"), HelloService.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 HelloService
*/
@WebEndpoint(name = "HelloServicePort")
public HelloService getHelloServicePort(WebServiceFeature... features) {
return super.getPort(new QName("http://myservice.cn/", "HelloServicePort"), HelloService.class, features);
}
private static URL __getWsdlLocation() {
if (HELLOSERVICESERVICE_EXCEPTION!= null) {
throw HELLOSERVICESERVICE_EXCEPTION;
}
return HELLOSERVICESERVICE_WSDL_LOCATION;
}
}
View Code
3.ObjectFactory.java

package cn.myservice;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the cn.myservice package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
private final static QName _SayResponse_QNAME = new QName("http://myservice.cn/", "sayResponse");
private final static QName _Say_QNAME = new QName("http://myservice.cn/", "say");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: cn.myservice
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link SayResponse }
*
*/
public SayResponse createSayResponse() {
return new SayResponse();
}
/**
* Create an instance of {@link Say }
*
*/
public Say createSay() {
return new Say();
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link SayResponse }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://myservice.cn/", name = "sayResponse")
public JAXBElement<SayResponse> createSayResponse(SayResponse value) {
return new JAXBElement<SayResponse>(_SayResponse_QNAME, SayResponse.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Say }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://myservice.cn/", name = "say")
public JAXBElement<Say> createSay(Say value) {
return new JAXBElement<Say>(_Say_QNAME, Say.class, null, value);
}
}
View Code
4.package-info.java

@javax.xml.bind.annotation.XmlSchema(namespace = "http://myservice.cn/") package cn.myservice;View Code
5.Say.java

package cn.myservice;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>say complex type的 Java 類。
*
* <p>以下模式片段指定包含在此類中的預期內容。
*
* <pre>
* <complexType name="say">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="arg0" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "say", propOrder = {
"arg0"
})
public class Say {
protected String arg0;
/**
* 獲取arg0屬性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getArg0() {
return arg0;
}
/**
* 設置arg0屬性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setArg0(String value) {
this.arg0 = value;
}
}
View Code
6.SayResponse.java

package cn.myservice;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>sayResponse complex type的 Java 類。
*
* <p>以下模式片段指定包含在此類中的預期內容。
*
* <pre>
* <complexType name="sayResponse">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayResponse")
public class SayResponse {
}
View Code
7.MyTest.java(測試類)

package cn.test;
import cn.myservice.HelloService;
import cn.myservice.HelloServiceService;
public class MyTest {
public static void main(String[] args) {
HelloServiceService service = new HelloServiceService();
HelloService port = service.getHelloServicePort();
port.say("坤坤");
}
}
View Code
8.運行效果

這就完成了調用。
2.使用CXF發布和調用web服務
未完待續。。。