package
test.jws.service;
import
javax.jws.WebMethod;
import
javax.jws.WebParam;
import
javax.jws.WebResult;
import
javax.jws.WebService;
import
javax.jws.soap.SOAPBinding;
@SOAPBinding
(style =
SOAPBinding
.Style.
RPC
)
public
class
HelloWorld {
@WebMethod
(action=
"toSayHello"
,operationName=
"toSayHello"
,exclude=
false
)
@WebResult
(name=
"returnWord"
)
//
自定義該方法返回值在
WSDL
中相關的描述
public
String sayHello(
@WebParam
(name=
"userName"
)String userName) {
return
"Hello:"
+ userName;
}
@WebMethod
public
int
getExp(
int
i,
int
j) {
return
i / j;
}
}
|
package
test.jws.service;
import
javax.xml.ws.Endpoint;
public
class
StartService {
public
static
void
main(String[] args) {
Endpoint.publish(
"http://localhost:8080/webservice/hws"
,
new
HelloWorld());
}
}
|
<?
xml version="1.0" encoding="UTF-8"
?>
<
definitions
xmlns
="
http://schemas.xmlsoap.org/wsdl/
"
xmlns:tns
="
http://www.jwstest.org
"
xmlns:xsd
="
http://www.w3.org/2001/XMLSchema
"
xmlns:soap
="
http://schemas.xmlsoap.org/wsdl/soap/
"
targetNamespace
="
http://www.jwstest.org
"
name
="
HelloWorldService
">
<
types
/>
<
message name
="
toSayHello
">
<part name="userName" type="xsd:string" />
</message>
<message name="toSayHelloResponse">
<part name="returnWord" type="xsd:string" />
</message>
<
message name
="
getExp
">
<part name="arg0" type="xsd:int" />
<part name="arg1" type="xsd:int" />
</message>
<
message name
="
getExpResponse
">
<part name="return" type="xsd:int" />
</message>
<
portType name
="
HelloWorld
">
<
operation name
="
toSayHello
"
parameterOrder
="
userName
">
<input message="tns:toSayHello" />
<output message="tns:toSayHelloResponse" />
</operation>
<
operation name
="
getExp
"
parameterOrder
="
arg0 arg1
">
<input message="tns:getExp" />
<output message="tns:getExpResponse" />
</operation>
</portType>
<
binding name
="
HelloWorldPortBinding
"
type
="
tns:HelloWorld
">
<soap:binding style="rpc"
transport
="
http://schemas.xmlsoap.org/soap/http
" />
<
operation name
="
toSayHello
">
<soap:operation soapAction="toSayHello" />
<
input
>
<soap:body use="literal"
namespace
="
http://www.jwstest.org
" />
</input>
<
output
>
<soap:body use="literal"
namespace
="
http://www.jwstest.org
" />
</output>
</operation>
<
operation name
="
getExp
">
<soap:operation soapAction="" />
<
input
>
<soap:body use="literal"
namespace
="
http://www.jwstest.org
" />
</input>
<
output
>
<soap:body use="literal"
namespace
="
http://www.jwstest.org
" />
</output>
</operation>
</
binding
>
<
service name
="
HelloWorldService
">
<port name="HelloWorldPort"
binding
="
tns:HelloWorldPortBinding
">
<soap:address
location
="
http://localhost:8080/webservice/hws
" />
</port>
</service>
</
definitions
>
|
package
test.jws.client;
import
test.jws.client.ref.*;
public
class
ClientRun {
/**
*
@param
args
*/
public
static
void
main(String[] args) {
HelloWorldService hws =
new
HelloWorldService();
HelloWorld hw = hws.getHelloWorldPort();
System.
out
.println(hw.getExp(9, 3));
System.
out
.println(hw.toSayHello(
"zhuoshiyao"
));
}
}
|
元素
|
定義
|
<portType>
|
web service 執行的操做
|
<message>
|
web service 使用的消息
|
<types>
|
web service 使用的數據類型
|
<binding>
|
web service 使用的通訊協議
|
<definitions>
<types>
definition of types........
</types>
<message>
definition of a message....
</message>
<portType>
definition of a port.......
</portType>
<binding>
definition of a binding....
</binding>
</definitions>
|
<message name="
getTermRequest
">
<part name="
term
" type="
xs:string
"/>
</message>
<message name="
getTermResponse
">
<part name="
value
" type="
xs:string
"/>
</message>
<portType name="
glossaryTerms
">
<operation name="
getTerm
">
<input message="
getTermRequest
"/>
<output message="
getTermResponse
"/>
</operation>
</portType>
|
類型
|
定義
|
One-way
|
此操做可接受消息,但不會返回響應。
|
Request-response
|
此操走可接受一個請求並會返回一個響應
|
Solicit-response
|
此操做可發送一個請求,並會等待一個響應。
|
Notification
|
此造做可發送一條消息,但不會等待響應。
|
<message name="
newTermValues
">
<part name="
term
" type="
xs:string
"/>
<part name="
value
" type="
xs:string
"/>
</message>
<portType name="
glossaryTerms
">
<operation name="
setTerm
">
<input name="
newTerm
" message="
newT
|