新建web項(xiàng)目,然后創(chuàng)建一個(gè)WEB服務(wù):
@WebService()

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

public String sayHi(@WebParam(name = "name")String name)
{
return "Hi " + name;
}
}
可以在源圖上右鍵,選Web服務(wù)--添加操作,也可以在設(shè)計(jì)圖上直接添加操作。@WebService標(biāo)注表明該類是一個(gè)web服務(wù),展現(xiàn)給web服務(wù)客戶端的業(yè)務(wù)方法必須使用@WebMethod標(biāo)注來表示。打包部署該web應(yīng)用,web服務(wù)自動(dòng)會(huì)發(fā)布。可以在glassfish應(yīng)用服務(wù)器上找到該web服務(wù),直接測試或者查看服務(wù)器生成的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>
也可以編寫客戶端測試,新建一個(gè)普通的java項(xiàng)目,在項(xiàng)目上右鍵,選擇新建--Web服務(wù)客戶端,在彈出窗口中指定WebService項(xiàng)目或者WSDL url,點(diǎn)擊完成。在源代碼上右鍵,選擇Web服務(wù)客戶端資源--調(diào)用Web服務(wù)操作,在彈出窗口中選擇sayHi操作,點(diǎn)確定,測試代碼自動(dòng)生成:

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
}
}
}
運(yùn)行該客戶端,結(jié)果將會(huì)輸出

]]>