新建web项目,然后创建一个WEB服务:
@WebService()
public class Hello {
@WebMethod(operationName = "sayHi")
public String sayHi(@WebParam(name = "name")String name) {
return "Hi " + name;
}
}
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 >
< 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
}
}
}
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
}
}
}
运行该客户端,结果将会输出