zoukankan      html  css  js  c++  java
  • Idea实现WebService实例 转

    作者:http://blog.csdn.net/dreamfly88/article/details/52350370

    因为工作需要,数据传输部分需要使用webservice实现,经过两天的研究,实现了一个简单的例子,具体方法如下。

    首先需要新建一个项目,如图:

    下一步点击finish,然后会生成一个webservice项目,在HelloWorld类里面写自己的方法,在file下编译一下这个类,不编译,idea会提示不通过,编译后需要将为该服务发布WSDL文件,此文件必须生成,如下图:

    选择需要发布的服务

    然后部署到TOMCAT,如图,这里需要注意的是需要引入这个库才能正常运行webservice

    启动tomcat后,在浏览器中敲入如下代码:localhost:8080/services 回车测试webservice是否部署成功:

    然后编写客户端测试代码,如下:

    主要代码:

    服务端:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package example;  
    2.   
    3. import javax.jws.WebService;  
    4.   
    5. /** 
    6.  * Created by zhangqq on 2016/8/26. 
    7.  */  
    8.   
    9. public class HelloWorld {  
    10.   
    11.   public String sayTitle(String from) {  
    12.     String result = "title is " + from;  
    13.     System.out.println(result);  
    14.     return result;  
    15.   }  
    16.   
    17.   
    18.   public String sayBody(String Other) {  
    19.     String result = "-------------body is-------------- " + Other;  
    20.     System.out.println(result);  
    21.     return result;  
    22.   }  
    23.   
    24.   public String sayAll(String title,String body) {  
    25.     String result ="--------title:"+title+ "----------------/r/nbody:--------------------------- " + body;  
    26.     System.out.println(result);  
    27.     return result;  
    28.   }  
    29. }  


    客户端:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package test;  
    2.   
    3. import org.apache.axis.client.Call;  
    4. import org.apache.axis.client.Service;  
    5. import org.apache.axis.utils.StringUtils;  
    6.   
    7. import javax.xml.rpc.ServiceException;  
    8. import java.net.MalformedURLException;  
    9.   
    10. /** 
    11.  * Created by zhangqq on 2016/8/29. 
    12.  */  
    13. public class WebSvrClient {  
    14.   
    15.   
    16.     public static void main(String[] args) {  
    17.         String url = "http://localhost:8080/services/HelloWorldService";  
    18.         String method = "sayTitle";  
    19.         String[] parms = new String[]{"abc"};  
    20.         WebSvrClient webClient = new WebSvrClient();  
    21.   
    22.         String svrResult = webClient.CallMethod(url, method, parms);  
    23.   
    24.         System.out.println(svrResult);  
    25.     }  
    26.   
    27.     public String CallMethod(String url, String method, Object[] args) {  
    28.         String result = null;  
    29.   
    30.         if(StringUtils.isEmpty(url))  
    31.         {  
    32.             return "url地址为空";  
    33.         }  
    34.   
    35.         if(StringUtils.isEmpty(method))  
    36.         {  
    37.             return "method地址为空";  
    38.         }  
    39.   
    40.         Call rpcCall = null;  
    41.   
    42.   
    43.         try {  
    44.             //实例websevice调用实例  
    45.             Service webService = new Service();  
    46.             rpcCall = (Call) webService.createCall();  
    47.             rpcCall.setTargetEndpointAddress(new java.net.URL(url));  
    48.             rpcCall.setOperationName(method);  
    49.   
    50.             //执行webservice方法  
    51.             result = (String) rpcCall.invoke(args);  
    52.   
    53.         } catch (Exception e) {  
    54.             e.printStackTrace();  
    55.         }  
    56.         return result;  
    57.   
    58.     }  
    59. }  



    实例地址:

    源码下载地址

  • 相关阅读:
    简单伪类
    购物网页css
    「WC2020T2」猜数
    ARC 103
    Codeforces 1198F
    ZJOI2019二试游记
    ZJOI2019一试游记
    「WC2015」未来程序
    「CodeForces Round #545 Div2」划水记
    「CF1116」Microsoft Q# Coding Contest
  • 原文地址:https://www.cnblogs.com/sekai/p/5952680.html
Copyright © 2011-2022 走看看