zoukankan      html  css  js  c++  java
  • 用axis做一个简单的webservice

    服务端

    服务端webserviceServer的代码如下:

    package server;
    
    public class HelloServer {
               public String sayHello(String name){
                    return "Hello! webservice...  "+name;
        }
    }

    在eclipse中,右键点击该类,选择webservice --> create webservice

    注意要选择public the web service

    finish之后, 服务端的代码写好了。 启动服务器,可以在浏览器中输入:http://localhost:8080/webserviceServer/services/HelloServer?wsdl  验证。

    客户端:

    webserviceClient

    到官网下载axis2取得jar包加入到lib中。

    第一中方式:

    package test;
    
    import javax.xml.namespace.QName;
    
    import org.apache.axis2.addressing.EndpointReference;
    import org.apache.axis2.client.Options;
    import org.apache.axis2.rpc.client.RPCServiceClient;
    
    public class Test {
        public static void main(String[] args) throws Exception{
        //  使用RPC方式调用WebService          
            RPCServiceClient serviceClient = new RPCServiceClient(); 
        
            Options options = serviceClient.getOptions();
        //  指定调用WebService的URL  
            EndpointReference targetEPR = new EndpointReference("http://localhost:8080/webserviceServer/services/HelloServer");
              options.setTo(targetEPR);  
            //  指定sayHello方法的参数值 
              Object[] opAddEntryArgs = new Object[] {"wangkun"}; 
              System.out.println(opAddEntryArgs[0]);
            //  指定sayHello方法返回值的数据类型的Class对象  
              Class[] classes = new Class[] {String.class}; 
            //  指定要调用的sayHello方法及WSDL文件的命名空间  
                   QName opAddEntry = new QName("http://server", "sayHello"); 
                   //System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
                   Object[] obj = serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs,classes);
                   System.out.println(obj[0]);
                   //System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs,classes));
        }
    }

    测试,如果结果出现Hello! webservice...  wangkun  则说明webservice 客户端代码调用了服务端。

    第二种方式:

    package test;
    import javax.xml.namespace.QName;
    import org.apache.axis.client.Call;
    import org.apache.axis.client.Service;
    public class Test2 {
        /**
         * @param args
         */
        public static void main(String[] args) throws Exception{
            // 服务端webservice地址
              String endpoint = "http://localhost:8080/webserviceServer/services/HelloServer";
              // 定义服务端webservice调用的方法名称opName
              String opName = "sayHello";
              WebServiceParam[] params = new WebServiceParam[1];
              // 可以定义多个WebServiceParam对象表示多个参数
              WebServiceParam p1 = new WebServiceParam(); 
              // 定义服务端webservice调用方法的参数名称以及值
              p1.setParamName("userId");
              p1.setParamValue("wangkun");
              QName qName = org.apache.axis.encoding.XMLType.XSD_STRING;
              p1.setqName(qName);
              System.out.println(p1.getqName());
              params[0] = p1;
              String str="";
              try {
               str = getStringByWebService(endpoint, opName, params);
               System.out.println("==webService Result==" + str);
              } catch (Exception e) {
               str = "异常";
               System.out.println("==webService error=str=" + str);
              }
        }
        
         /**
          * <p>
          * Description:调用返回值是string类型的webservice
          * <p>
          * @param endpoint
          * @param opName
          * @param params
          */
         public static String getStringByWebService(String endpoint, String opName,
           WebServiceParam[] params) throws Exception {
          try {
           String res = null;
           Service service = new Service();
           Call call = (Call) service.createCall();
           call.setTargetEndpointAddress(new java.net.URL(endpoint));
           for (int i = 0; i < params.length; i++) {
            call.addParameter(params[i].getParamName(), params[i].getqName(),
              javax.xml.rpc.ParameterMode.IN);
           }
           call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
           call.setOperationName(new QName(endpoint + opName,
             opName));
           call.setUseSOAPAction(true);
           call.setSOAPVersion(org.apache.axis.soap.SOAPConstants.SOAP11_CONSTANTS);
           Object o[] = new Object[params.length];
           for (int i = 0; i < params.length; i++) {
            o[i] = params[i].getParamValue();
           }
           res = (String) call.invoke(o);
           System.out.println(res);
           return res;
          } catch (Exception e) {
           throw e;
          }
         }
    }
    
    class WebServiceParam{
        private String paramName;
         private Object paramValue;
         private QName qName;
         
         public WebServiceParam(){}
         public WebServiceParam(String paramName,String paramValue,QName qName){
          this.paramName = paramName;
          this.paramValue = paramValue;
          this.qName = qName;
         }
         public QName getqName() {
          return qName;
         }
         public void setqName(QName qName) {
          this.qName = qName;
         }
         public String getParamName() {
          return paramName;
         }
         public void setParamName(String paramName) {
          this.paramName = paramName;
         }
         public Object getParamValue() {
          return paramValue;
         }
         public void setParamValue(Object paramValue) {
          this.paramValue = paramValue;
         }
    }
  • 相关阅读:
    phpcms后台进入地址(包含No permission resources错误)
    phpmyadmin上传大sql文件办法
    ubuntu彻底卸载mysql
    Hdoj 2602.Bone Collector 题解
    一篇看懂词向量
    Hdoj 1905.Pseudoprime numbers 题解
    The Python Challenge 谜题全解(持续更新)
    Hdoj 2289.Cup 题解
    Hdoj 2899.Strange fuction 题解
    Hdoj 2199.Can you solve this equation? 题解
  • 原文地址:https://www.cnblogs.com/kunpengit/p/2610332.html
Copyright © 2011-2022 走看看