标签:java webservice
1:从WebService的工作模式上理解的话,它跟普通的Web程序(比如ASP、JSP等)并没有本质的区别,都是基于HTTP传输协议的程序。
2:WebService所使用的数据均是基于XML格式的。目前标准的WebService在数据格式上主要采用SOAP协议。SOAP协议实际上就是一种基于XML编码规范的文本协议。
在JDK1.6中JAX-WS规范定义了如何发布一个webService服务。
JAX-WS是指JavaApi for XML –WebService.
/** * 将 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..."); } }
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,0,len,"UTF-8"); sb.append(str); } System.out.println(sb.toString()); is.close(); } con.disconnect(); } }
通过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()); } }
标签:java webservice
原文地址:http://blog.csdn.net/ankeyuan/article/details/38616455