WebService,顾名思义就是基于Web的服务。它使用Web(HTTP)方式,接收和响应外部系统的某种请求。从而实现远程调用.
1:从WebService的工作模式上理解的话,它跟普通的Web程序(比如ASP、JSP等)并没有本质的区别,都是基于HTTP传输协议的程序。
2:WebService所使用的数据均是基于XML格式的。目前标准的WebService在数据格式上主要采用SOAP协议。SOAP协议实际上就是一种基于XML编码规范的文本协议。
名词1:WSDL – WebService Description Language – Web服务描述语言。
•通过XML形式说明服务在什么地方-地址。
名词2:SOAP-Simple Object Access Protocol(简单对象访问协议)
•SOAP = 在HTTP的基础上+XML数据。
•SOAP是基于HTTP的。
•SOAP的组成如下:
•Envelope – 必须的部分。以XML的根元素出现。
•Headers – 可选的。
•Body – 必须的。在body部分,包含要执行的服务器的方法。和发送到服务器的数据。
在Java项目中发布第一个WS服务:
在JDK1.6中JAX-WS规范定义了如何发布一个webService服务。
JAX-WS是指JavaApi for XML –WebService.
1.用Jdk1.6.0_21以后的版本发布一个WebService服务.
2.与Web服务相关的类,都位于javax.jws.*包中。
主要类有:
/** * 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口 * */ @WebService(targetNamespace="http://www.usst.cn",serviceName="MyService") public class HelloService { @WebMethod(operationName="hello") @WebResult(name="return") public String sayHello( @WebParam(name="name") String name,@WebParam(name="age") int age){ System.out.println("sayHello called..."); return "hello " + name; } @WebMethod(exclude=true) public String sayHello2(String name){ System.out.println("sayHello called..."); return "hello " + name; } public static void main(String[] args) { //参数1:绑定服务的地址 参数2:提供服务的实例 Endpoint.publish("http://192.168.1.101:5678/hello",new HelloService()); System.out.println("server ready..."); } }
•static Endpoint.publish(String address,Object implementor)
在给定地址处针对指定的实现者对象创建并发布端点。
在给定地址处针对指定的实现者对象创建并发布端点。
•stop方法用于停止服务。
•EndPoint发布完成服务以后,将会独立的线程运行。所以,publish之后的代码,可以正常执行。
其他注意事项:
客户端调用WebService的方式
wsimport -s . http://192.168.1.101:5678/hello
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 soap = hss.getHelloServicePort(); String str = soap.sayHello("zhangsan"); System.out.println(str); System.out.println(soap.getClass().getName()); } }
<html> <head> <title>通过ajax调用webservice服务</title> <script> var xhr; function sendAjaxWS(){ xhr = new ActiveXObject("Microsoft.XMLHTTP"); //指定ws的请求地址 var wsUrl = "http://10.10.49.16:5678/hello"; //手动构造请求体 var requestBody = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ' + ' xmlns:q0="http://service.usst.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema "'+ ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'+ '<soapenv:Body><q0:sayHello><arg0>'+document.getElementById("msg").value+'</arg0> <arg1>10</arg1> </q0:sayHello></soapenv:Body></soapenv:Envelope>'; //打开连接 xhr.open("POST",wsUrl,true); //重新设置请求头 xhr.setRequestHeader("content-type","text/xml;charset=utf8"); //设置回调函数 xhr.onreadystatechange = _back; //发送请求 xhr.send(requestBody); } //定义回调函数 function _back(){ if(xhr.readyState == 4){ if(xhr.status == 200){ var ret = xhr.responseXML; //解析xml var eles = ret.getElementsByTagName("return")[0]; alert(eles.text); } } } </script> </head> <body> <input type="text" id="msg" /> <input type="button" onclick="sendAjaxWS();" value="通过ajax调用webservice服务"/> </body> </html>
public class App { public static void main(String[] args) throws Exception { // 指定webservice服务的请求地址 String wsUrl = "http://192.168.1.108:5678/hello"; URL url = new URL(wsUrl); URLConnection conn = url.openConnection(); HttpURLConnection con = (HttpURLConnection) conn; // 设置请求方式 con.setDoInput(true); con.setDoOutput(true); con.setRequestMethod("POST"); con.setRequestProperty("content-type","text/xml;charset=UTF-8"); // 手动构造请求体 String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " + " xmlns:q0=\"http://service.usst.cn/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema \" " + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "<soapenv:Body><q0:sayHello><arg0>lisi</arg0> <arg1>10</arg1> </q0:sayHello></soapenv:Body></soapenv:Envelope>"; //获得输出流 OutputStream out = con.getoutputStream(); out.write(requestBody.getBytes()); out.close(); int code = con.getResponseCode(); if(code == 200){//服务端返回正常 InputStream is = con.getInputStream(); byte[] b = new byte[1024]; StringBuffer sb = new StringBuffer(); int len = 0; while((len = is.read(b)) != -1){ String str = new String(b,len,"UTF-8"); sb.append(str); } System.out.println(sb.toString()); is.close(); } con.disconnect(); } }
l通过客户端编程的方式调用
通过wsimport -s -p cn.usst.service . http://192.168.1.101:5678/hello导入接口类 public class App { public static void main(String[] args) throws Exception { Service s = Service.create(new URL("http://192.168.1.101:5678/hello?wsdl"),new QName("http://service.usst.cn/","HelloServiceService")); HelloService hs = s.getPort(new QName("http://service.usst.cn/","HelloServicePort"),HelloService.class); String str = hs.sayHello("lisi",10); System.out.println(str); System.out.println(hs.getClass().getSimpleName()); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。