Java中webService的几种调用方法

来源:互联网 发布:vscode的php插件 编辑:程序博客网 时间:2024/06/08 16:46

第一种,HttpClient模拟webService

首先,导入jar包commons-httpclient-3.1.jar,准备模拟请求的基本数据sb

其次,封装请求request对象

再次,发送请求并接受返回结果

最后,得到的结果是XML形式的字符窜,可以用DoM,或sax技术进行报文解析。

形式1:

1

形式2:

public class test{

 public static void main(String[] args) throws Exception {

HttpClient httpclient = new HttpClient();

String url = “http://locahost:9999/ws”;

HttpPost httppost = new HttpPost(url);

                 //定义json对象,里面放请求报文体

JSONObject json = new JSONObject();

               //以下内容根据报文格式决定

                json.put("user","zhangsan" );

                 json.put("pwd","123456" );
        json.put("msgtype", "text");//格式

               。。。。。

              JSONObject detailA = new JSONObject();
detailA.put("title", "xxxxxx”);// 非必须,标题
detailA.put("content", "正文");// 必须,正文
json.put("text", detailA);

              。。。。

             StringEntity params = new StringEntity(json.toString(), "UTF-8");
     httppost.setEntity(params);
    HttpResponse response = httpclient.execute(httppost);//获得响应

            int code=response.getStatusLine().getStatusCode() //获得响应吗,200为请求成功,作为判断使用

           /* 读返回数据 */
   String conResult = EntityUtils.toString(response.getEntity());
   JSONObject sobj = new JSONObject();
  sobj = sobj.fromObject(conResult);
  String errmsg = sobj.getString("errmsg");
  String errcode = sobj.getString("errcode");

          。。。。

         以下根据业务需求。。。。。

 }

}

第二种,Jax_wbservice标准调用方式:服务器端地址改变,方便改




第三种,通过CXF调用web服务

1、 先通过(*wsimport 生成客户端)


2、把生成的代码拷贝到你的程序中

3、导入CXF的依赖jar包,


4、调用,com.zhj.service包就是通过import生成的文件包,拷贝到项目中。



下面说一说CXF+Spring调用webservice:

Ø导入springcxf的依赖jar:
Øwsimport生成客户端代码,拷贝到本项目
ØSpring配置文件中增加配置,将服务客户端纳入工厂


web.xml中配置:


第四种、CXF RestFul风格的web service调用。
优点:是不需要生成客户端代码,只需要知道接口发布的网址即可
缺点:服务器端必须也是用RestFul风格发布的。
详情请查看我本博客中的另一篇文章:
Java CXF RestFul风格的web service发布与调用
http://blog.csdn.net/zhangyunfeixyz/article/details/72912244

第五种、axis2远程调用webservice

  1. TestClient.java  
  2. package example.client;  
  3.    
  4. import org.apache.axiom.om.OMAbstractFactory;  
  5. import org.apache.axiom.om.OMElement;  
  6. import org.apache.axiom.om.OMFactory;  
  7. import org.apache.axiom.om.OMNamespace;  
  8. import org.apache.axis2.addressing.EndpointReference;  
  9. import org.apache.axis2.client.Options;  
  10. import org.apache.axis2.client.ServiceClient;  
  11.    
  12. public class TestClient {  
  13.     // targetEPR指定打包的Service(.aar文件)在容器中的物理位置。  
  14.        private static EndpointReference targetEPR=new EndpointReference  
  15.           ("http://localhost:8080/axis2/services/HelloWorld");  
  16.        public static OMElement getSayHelloOMElement(){  
  17.         //创建request SOAP包。  
  18.                OMFactory fac=OMAbstractFactory.getOMFactory();  
  19.         // OMNamespace指定此SOAP文档名称空间。  
  20.                OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");  
  21.         //创建元素sayHello,并指定其在omNs指代的名称空间中。  
  22.                OMElement method=fac.createOMElement("sayHello",omNs);  
  23.         //指定元素的文本内容。  
  24.                method.setText("ZJ");  
  25.               return method;  
  26.         }  
  27.        public static void main(String[] args){  
  28.               try{  
  29.                       Options options=new Options();  
  30.                       options.setTo(targetEPR);  
  31.                       ServiceClient sender=new ServiceClient();  
  32.                       sender.setOptions(options);  
  33.                       OMElement sayHello=TestClient.getSayHelloOMElement();  
  34.             //发出request SOAP,  
  35. //同时将得到的远端由sayHello方法返回的信息保存到result。  
  36. //通过services.xml能准确找到sayHello方法所在的文件。  
  37.                       OMElement result=sender.sendReceive(sayHello);  
  38.                }  
  39.               catch(Exception axisFault){  
  40.                       axisFault.printStackTrace();  
  41.                }  
  42.         }  
  43. }

第六种、xfire的发布与调用,这里不说了,网上一搜一片,照着学习就好了。


原创粉丝点击