webService 学习记录
根据网上视频学习整理,如有不对的地方,请大神们不吝赐教。
1.webService 是应用程序之间的远程调用,可以跨语言;
3.需要了解的几个名词
①XML,
②WSDL(WebServiceDescription Language) – Web服务描述语言。
可以理解为WebService的使用说明书,说明服务在哪里,如何调用
通过XML形式说明服务提供什么样的方法– 如何调用
如下的WSDL文件
<?xml version="1.0" encoding="UTF8" ?>
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://ws.itcast.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.itcast.cn/"
name="HelloServiceService">
<types>
<xsd:schema>
<xsd:import namespace="http://ws.itcast.cn/"
schemaLocation="http://localhost:9999/hello?xsd=1" />
</xsd:schema>
</types>
<message name="sayHi">
<part name="parameters" element="tns:sayHi" />
</message>
<message name="sayHiResponse">
<part name="parameters" element="tns:sayHiResponse" />
</message>
<portType name="HelloService">
<operation name="sayHi">
<input message="tns:sayHi" />
<output message="tns:sayHiResponse" />
</operation>
</portType>
<binding name="HelloServicePortBinding" type="tns:HelloService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"
style="document" />
<operation name="sayHi">
<soap:operation soapAction="" />
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
</binding>
<service name="HelloServiceService">
<port name="HelloServicePort" binding="tns:HelloServicePortBinding">
<soap:address location="http://localhost:9999/hello" />
</port>
</service>
</definitions>
③SOAP-Simple Object Access Protocol(简单对象访问协议)
POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: webservice.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<getMobileCodeInfo xmlns="http://WebXml.com.cn/">
<mobileCode>string</mobileCode>
<userID>string</userID>
</getMobileCodeInfo>
</soap:Body>
</soap:Envelope>
package cn.itcast.ws;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService /*需要加注解,只有加了注解的类,才能发布为Web服务*/
public class HelloService {
/**
* @param args
*/
public static void main(String[] args) {
/*发布方法
参数1:服务的地址
参数2:服务的实现者
*/
Endpoint.publish("http://127.0.0.1:6789/hello", new HelloService());
}
public String sayHello(String name){
return "Hello" +name;
}
}
出现的错误:未加sayHello方法之前会出现错误
Exception in thread "main" com.sun.xml.internal.ws.model.RuntimeModelerException: The web service defined by the class cn.itcast.ws.HelloService does not contain any valid WebMethods.
at com.sun.xml.internal.ws.model.RuntimeModeler.buildRuntimeModel(Unknown Source)
at com.sun.xml.internal.ws.server.EndpointFactory.createSEIModel(Unknown Source)
at com.sun.xml.internal.ws.server.EndpointFactory.createEndpoint(Unknown Source)
at com.sun.xml.internal.ws.api.server.WSEndpoint.create(Unknown Source)
at com.sun.xml.internal.ws.api.server.WSEndpoint.create(Unknown Source)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.createEndpoint(Unknown Source)
at com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(Unknown Source)
at com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(Unknown Source)
at javax.xml.ws.Endpoint.publish(Unknown Source)
at cn.itcast.ws.HelloService.main(HelloService.java:17)
因为在java中没有对外提供有效的方法,main方法的static的不能被外部调用发布的,不支持static
的,final方法,我们可以提供一个有效的方法对外发布(sayHello)方法,
运行程序就可以,
那么怎么看发布的WSDL文件呢?
拷贝发布的地址,找浏览器打开,粘贴在发布地之后加?wsdl即可。如:http://127.0.0.1:6789/hello?wsdl,就可以看本服务的WSDL文件;
服务发布之后,剩下的就是客户端调用的问题了
Endpoint.publish()方法执行时会启动一个新的线程,用于监听客户端的请求,之后的方法也会执行,多线程执行
若暂时不想将某一方法发布,可以在方法前加@WebMethod(exclude = true)注解
5.客户端如何调用服务费发布的方法
①通过wsimport生成本地代码,调用网络上的web服务
客户端代码我们可以用wsimport命令自动生成,拷贝到eclipse中然后调用即可
示例:
C:/>wsimport –s .http://192.168.0.100/one?wsdl
注意:-s不能分开,-s后面有个小点,用于指定源代码生成的目录。点即当前目录。
如果使用了-s参数则会在目录下生成两份代码,一份为.class代码。一份为.java代码。
.class代码,可以经过打包以后使用。.java代码可以直接Copy到我们的项目中运行。
出的代码中会按照默认的包结构进行导出,如想自己定义包结构可以用 wsimport -s . -p com.test http://192.168.0.100/one?wsdl命令。
将生成的客户端代码拷贝到eclipse中之后,可以自己定义java类,调用发布的方法。
代码如下:
package cn.itcast.ws;
/**
* 通过wsimport命令生产客户端代码调用webService服务
* @author wangz_ing
*
*/
public class App {
public static void main(String[] args) {
/**
* WSDL
* -<service name="HelloServiceService">
*/
HelloServiceService hss = new HelloServiceService();
/**
* WSDL
* -<port name="HelloServicePort" binding="tns:HelloServicePortBinding">
*/
HelloService hs = hss.getHelloServicePort();
String ret = hs.sayHello("jack:");
System.out.println(ret);
System.out.println(hs.getClass().getName());
}
}