JDK中已经内置了Webservice发布,使用JAX-WS通过java application的方式调用Endpoint类的静态方法publish发布webservice,这种方法使用起来虽然简单,但是对于一个企业级应用来说通常对外提供的服务可能不止一个,如果每个WebService都去编写一个main方法显然不合适,不利于Web服务的集中管理。
所以绝大多数情况下都会使用Web容器集中管理WebService,要用Tomcat等Web服务器发布WebService,就需要用第三方Webservice框架。Axis2和CXF是目前最流行的Webservice框架,这两个框架各有优点,不过都属于重量级框架。
个人觉得使用JAX-WS基于Web应用服务器来发布WebService比较轻量级,前面看过一篇文章对整合做了具体的整体贴一下地址:
以上是有关发布的认识,然后是调用webservice的认识:
一、生成客户端调用方式
第一步:wsimport生成客户端代码
wsimport -p cn.itcast.mobile -s .http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
第二步:阅读使用说明书,使用生成客户端代码调用服务端
该种方式使用简单,但一些关键的元素在代码生成时写死到生成代码中,不方便维护,所以仅用于测试。
二、service编程调用方式
第一步:使用wsimport或其它工具(比如cxf的wsdl2java)生成客户端的调用
第二步:使用url、qname、service调用服务端
package cn.itcast.mobile.client; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import cn.itcast.mobile.MobileCodeWSSoap; /** * * <p>Title: ServiceClient.java</p> * <p>Description:Service编程实现服务端调用</p> * */ public class ServiceClient { public static void main(String[] args) throws IOException { //创建WSDL的URL,注意不是服务地址 URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"); //创建服务名称 //1.namespaceURI - 命名空间地址 //2.localPart - 服务视图名 QName qname = new QName("http://WebXml.com.cn/", "MobileCodeWS"); //创建服务视图 //参数解释: //1.wsdlDocumentLocation - wsdl地址 //2.serviceName - 服务名称 Service service = Service.create(url, qname); //获取服务实现类 MobileCodeWSSoap mobileCodeWSSoap = service.getPort(MobileCodeWSSoap.class); //调用查询方法 String result = mobileCodeWSSoap.getMobileCodeInfo("1866666666", ""); System.out.println(result); } }
特点
该种方式可以自定义关键元素,方便以后维护,是一种标准的开发方式
三、HttpURLConnection调用方式
使用HttpURLConnection或apache的Httpclient模拟http请求,调用webservice。
开发步骤:
第一步:创建服务地址
第二步:打开一个通向服务地址的连接
第三步:设置参数
设置POST,POST必须大写,如果不大写,报如下异常
第四步:组织SOAP数据,发送请求
第五步:接收服务端响应,打印
package cn.itcast.mobile.client; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; /** * * <p>Title: HttpClient.java</p> * <p>Description:HttpURLConnection调用方式</p> */ public class HttpClient { public static void main(String[] args) throws IOException { //第一步:创建服务地址,不是WSDL地址 URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"); //第二步:打开一个通向服务地址的连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //第三步:设置参数 //3.1发送方式设置:POST必须大写 connection.setRequestMethod("POST"); //3.2设置数据格式:content-type connection.setRequestProperty("content-type", "text/xml;charset=utf-8"); //3.3设置输入输出,因为默认新创建的connection没有读写权限, connection.setDoInput(true); connection.setDoOutput(true); //第四步:组织SOAP数据,发送请求 String soapXML = getXML("15226466316"); OutputStream os = connection.getOutputStream(); os.write(soapXML.getBytes()); //第五步:接收服务端响应,打印 int responseCode = connection.getResponseCode(); if(200 == responseCode){//表示服务端响应成功 InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String temp = null; while(null != (temp = br.readLine())){ sb.append(temp); } System.out.println(sb.toString()); is.close(); isr.close(); br.close(); } os.close(); } /** * <?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> * @param phoneNum * @return */ public static String getXML(String phoneNum){ String soapXML = "<?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>"+phoneNum+"</mobileCode>" +"<userID></userID>" +"</getMobileCodeInfo>" +"</soap:Body>" +"</soap:Envelope>"; return soapXML; } }
四、Ajax调用方式
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript"> function queryMobile(){ //创建XMLHttpRequest对象 var xhr = new XMLHttpRequest(); //打开连接 xhr.open("post","http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx",true); //设置数据类型 xhr.setRequestHeader("content-type","text/xml;charset=utf-8"); //设置回调函数 xhr.onreadystatechange=function(){ //判断是否发送成功和判断服务端是否响应成功 if(4 == xhr.readyState && 200 == xhr.status){ alert(xhr.responseText); } } //组织SOAP协议数据 var soapXML = "<?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>"+document.getElementById("phoneNum").value+"</mobileCode>" +"<userID></userID>" +"</getMobileCodeInfo>" +"</soap:Body>" +"</soap:Envelope>"; alert(soapXML); //发送数据 xhr.send(soapXML); } </script> </head> <body> 手机号查询:<input type="text" id="phoneNum"/> <input type="button" value="查询" onclick="javascript:queryMobile();"/> </body> </html>