新建web項目,然后創建一個WEB服務:
@WebService()

public class Hello
{
@WebMethod(operationName = "sayHi")

public String sayHi(@WebParam(name = "name")String name)
{
return "Hi " + name;
}
}
可以在源圖上右鍵,選Web服務--添加操作,也可以在設計圖上直接添加操作。@WebService標注表明該類是一個web服務,展現給web服務客戶端的業務方法必須使用@WebMethod標注來表示。打包部署該web應用,web服務自動會發布。可以在glassfish應用服務器上找到該web服務,直接測試或者查看服務器生成的WSDL
<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3.1-hudson-417-SNAPSHOT. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3.1-hudson-417-SNAPSHOT. -->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice/" name="HelloService">
<types>
<xsd:schema>
<xsd:import namespace="http://webservice/" schemaLocation="http://localhost:8080/WebServiceApp/HelloService?xsd=1">
</xsd:import>
</xsd:schema>
</types>
<message name="sayHi">
<part name="parameters" element="tns:sayHi">
</part>
</message>
<message name="sayHiResponse">
<part name="parameters" element="tns:sayHiResponse">
</part>
</message>
<portType name="Hello">
<operation name="sayHi">
<input message="tns:sayHi">
</input>
<output message="tns:sayHiResponse">
</output>
</operation>
</portType>
<binding name="HelloPortBinding" type="tns:Hello">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document">
</soap:binding>
<operation name="sayHi">
<soap:operation soapAction="">
</soap:operation>
<input>
<soap:body use="literal">
</soap:body>
</input>
<output>
<soap:body use="literal">
</soap:body>
</output>
</operation>
</binding>
<service name="HelloService">
<port name="HelloPort" binding="tns:HelloPortBinding">
<soap:address location="http://localhost:8080/WebServiceApp/HelloService">
</soap:address>
</port>
</service>
</definitions>
也可以編寫客戶端測試,新建一個普通的java項目,在項目上右鍵,選擇新建--Web服務客戶端,在彈出窗口中指定WebService項目或者WSDL url,點擊完成。在源代碼上右鍵,選擇Web服務客戶端資源--調用Web服務操作,在彈出窗口中選擇sayHi操作,點確定,測試代碼自動生成:

public class Main
{


public static void main(String[] args)
{


try
{

webservice.HelloService service = new webservice.HelloService();
webservice.Hello port = service.getHelloPort();
java.lang.String name = "Tom";
java.lang.String result = port.sayHi(name);
System.out.println("Result = " + result);

} catch (Exception ex)
{
// TODO handle custom exceptions here
}
}
}
運行該客戶端,結果將會輸出

]]>