1.服务端:
package zhou;
import javax.jws.WebService;
/*
* 可写成接口形式去实现
* 实现类必须指定接口包路径
* @WebService(endpointInterface = "...")
* */
@WebService(targetNamespace = "http://192.168.0.108:8099/hello")
public class PersonService {
public String sayHello(String name) {
return name + "hello";
}
}
2. 启动服务端调用:
import zhou.PersonService;
import javax.xml.ws.Endpoint;
public class Publisher {
public static void main(String[] args) {
Endpoint publish = Endpoint.publish("http://192.168.0.108:8099/hello", new PersonService());
}
}
浏览器访问:
http://192.168.0.108:8099/hello?wsdl
WSDL:webservice描述语言
读的方式:从下向上
<!--
Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<!--
Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"
xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://192.168.0.108:8099/hello"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="http://192.168.0.108:8099/hello" name="PersonServiceService">
<!--
types:对输入输出的消息体的约束schema
-->
<types>
<xsd:schema>
<xsd:import namespace="http://192.168.0.108:8099/hello"
schemaLocation="http://192.168.0.108:8099/hello?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"/>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"/>
</message>
<!--
portType:具体的服务类的元素,在portType内部有多个operation,operation就是服务类中的方法
message:输入输出的消息体,分别对应着服务端接收的参数和响应的返回值,消息是以soap为协议,
这个协议是http方式的请求,请求体是xml,这个xml不能随便写,需要约束使用schema
-->
<portType name="PersonService">
<operation name="sayHello">
<input wsam:Action="http://192.168.0.108:8099/hello/PersonService/sayHelloRequest" message="tns:sayHello"/>
<output wsam:Action="http://192.168.0.108:8099/hello/PersonService/sayHelloResponse"
message="tns:sayHelloResponse"/>
</operation>
</portType>
<!--
binding:把服务访问点和服务类绑定在一起,不需要关心元素的内部,要关心的是type属性,这个属性指定了绑定了服务类的xml元素(portType)
-->
<binding name="PersonServicePortBinding" type="tns:PersonService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<!--服务访问点的集合,可以有多个
元素的name属性很重要,调用时需要使用
-->
<service name="PersonServiceService">
<!--port:服务访问点,元素的name属性调用时需要使用
binding:属性指定了服务访问点和服务类绑定元素
-->
<port name="PersonServicePort" binding="tns:PersonServicePortBinding">
<soap:address location="http://192.168.0.108:8099/hello"/>
</port>
</service>
</definitions>
XSD:http://192.168.0.108:8099/hello?xsd=1
<!--
Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<xs:schema xmlns:tns="http://192.168.0.108:8099/hello" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"
targetNamespace="http://192.168.0.108:8099/hello">
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element name="arg0" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element name="return" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
消息体:请求与响应 (IDEA需要装SOAP的插件)