Java调用WebService的方法总结

1、使用命令wsimport自动生成java代码

         wsimport是jdk自带的,可以根据wsdl文档生成客户端调用代码的工具.

    wsimport.exe位于JAVA_HOME\bin目录下。

   常用参数为:
    •-d<目录>  - 将生成.class文件。默认参数。
    •-s<目录> - 将生成.java文件。
    •-p<生成的新包名> -将生成的类,放于指定的包下。
    •(wsdlurl) - http://server:port/service?wsdl,必须的参数。

   示例:

      wsimport –s . http://192.168.0.100/one?wsdl

  注意:-s不能分开,-s后面有个小点,用于指定源代码生成的目录。点即当前目录。

  如果使用了-s参数则会在目录下生成两份代码,一份为.class代码。一份为.java代码。

  .class代码,可以经过打包以后使用。.java代码可以直接Copy到我们的项目中运行。

项目示例

使用这种方法需要稍微能看懂WSDL,代码和WDSL的对应关系如上图所示。中间部分为自己要写的代码,左上角为源文件,除了Main.java其他的都市自动生成的。

 

 

 

2、使用AXIS调用WebService

     需要用到的jar包:   

  • axis-1_2/lib/axis.jar
  • axis-1_2/lib/jaxrpc.jar
  • axis-1_2/lib/saaj.jar
  • axis-1_2/lib/commons-logging.jar
  • axis-1_2/lib/commons-discovery.jar
  • axis-1_2/lib/wsdl4j.jar

示例代码:

  个人理解:Axis根据WSDL获取WebService的基本信息,然后程序员设置xml代码和java代码的对应关系,Axis自动发送请求和解析响应;

        因为在WebService中java类时经过序列化的,所以传递参数不是基本数据类型时,要指定序列化对应关系,就是代码25、26行,这时候反序列化器可以为空;

        当然当返回消息为java对象时(java对象我一般用wsimport方法自动生成),要指定将XML反序列化成java对象的对应关系,就是代码72、73行,这时序列化器可以为空;

  1 /**
  2      * 第二种方法:Axis方法调用
  3      */
  4     public static void axis_01() {
  5         try {
  6             String endpoint = "http://127.0.0.1:8089/BaoIntlWebService/ws/binding";
  7 
  8             /*
  9              * Create new Service and Call objects. These are the standard
 10              * JAX-RPC objects that are used to store metadata about the service
 11              * to invoke.
 12              */
 13             Service service = new Service();
 14             Call call = (Call) service.createCall();
 15 
 16             // Set up endpoint URL - this is the destination for our SOAP
 17             // message.
 18             call.setTargetEndpointAddress(new java.net.URL(endpoint));
 19 
 20             // 设置调用WS的方法
 21             call.setOperationName(new QName("http://webservice.baointl.com/", "mapTest"));
 22 
 23             // 注册序列化/反序列化器
 24             QName qn_request = new QName("http://webservice.baointl.com/", "requestTest");
 25             call.registerTypeMapping(RequestTest.class, qn_request, new BeanSerializerFactory(
 26                     RequestTest.class, qn_request), new BeanDeserializerFactory(RequestTest.class,
 27                     qn_request));
 28 
 29             // 设置参数名
 30             call.addParameter("request", qn_request, RequestTest.class, ParameterMode.IN);
 31 
 32             // 设置返回类型
 33             call.setReturnType(org.apache.axis.Constants.XSD_STRING);
 34 
 35             RequestTest request = new RequestTest();
 36             request.setName("LEO");
 37             request.setAge(28);
 38 
 39             Object ret = call.invoke(new Object[] { request });
 40 
 41             System.out.println("got '" + ret + "'");
 42         } catch (RemoteException e) {
 43             e.printStackTrace();
 44         } catch (ServiceException e) {
 45             e.printStackTrace();
 46         } catch (MalformedURLException e) {
 47             e.printStackTrace();
 48         }
 49     }
 50 
 51     public static void axis_02() {
 52         try {
 53             String endpoint = "http://127.0.0.1:8089/BaoIntlWebService/ws/binding";
 54 
 55             /*
 56              * Create new Service and Call objects. These are the standard
 57              * JAX-RPC objects that are used to store metadata about the service
 58              * to invoke.
 59              */
 60             Service service = new Service();
 61             Call call = (Call) service.createCall();
 62 
 63             // Set up endpoint URL - this is the destination for our SOAP
 64             // message.
 65             call.setTargetEndpointAddress(new java.net.URL(endpoint));
 66 
 67             // 设置调用WS的方法
 68             call.setOperationName(new QName("http://webservice.baointl.com/", "push"));
 69 
 70             // 注册序列化/反序列化器
 71             QName qn = new QName("http://webservice.baointl.com/", "pushRespMsg");
 72             call.registerTypeMapping(PushRespMsg.class, qn, null, new BeanDeserializerFactory(
 73                     PushRespMsg.class, qn));
 74 
 75             // 设置参数名:
 76             call.addParameter("username", // 参数名
 77                     org.apache.axis.Constants.XSD_STRING,// 参数类型:String
 78                     ParameterMode.IN);// 参数模式:'IN' or 'OUT'
 79             call.addParameter("segno", // 参数名
 80                     org.apache.axis.Constants.XSD_STRING,// 参数类型:String
 81                     ParameterMode.IN);// 参数模式:'IN' or 'OUT'
 82             call.addParameter("first", // 参数名
 83                     org.apache.axis.Constants.XSD_STRING,// 参数类型:String
 84                     ParameterMode.IN);// 参数模式:'IN' or 'OUT'
 85             call.addParameter("keyword1", // 参数名
 86                     org.apache.axis.Constants.XSD_STRING,// 参数类型:String
 87                     ParameterMode.IN);// 参数模式:'IN' or 'OUT'
 88             call.addParameter("keyword2", // 参数名
 89                     org.apache.axis.Constants.XSD_STRING,// 参数类型:String
 90                     ParameterMode.IN);// 参数模式:'IN' or 'OUT'
 91             call.addParameter("remark", // 参数名
 92                     org.apache.axis.Constants.XSD_STRING,// 参数类型:String
 93                     ParameterMode.IN);// 参数模式:'IN' or 'OUT'
 94             // 设置返回值类型
 95             call.setReturnClass(PushRespMsg.class);
 96 
 97             Object[] object = new Object[6];
 98             object[3] = "11111";
 99             object[4] = "22222";
100             object[5] = "mark";
101             PushRespMsg result = (PushRespMsg) call.invoke(object);
102             System.out.println(result.getStatus());
103             System.out.println(result.getMessage());
104             System.out.println(result.getSuccessNum());
105             System.out.println(result.getFialedNum());
106         } catch (RemoteException e) {
107             e.printStackTrace();
108         } catch (ServiceException e) {
109             e.printStackTrace();
110         } catch (MalformedURLException e) {
111             e.printStackTrace();
112         }
113     }

 3、使用AXIS2调用WebService

  3.1 Axis2 DataBinding Framework

  3.2 XMLBeans

  3.3 JiBX databinding

posted @ 2014-11-04 18:11  庚午月圆人  阅读(3609)  评论(0编辑  收藏  举报