首页 > Web开发 > 详细

webservice的简单实现发布和客户端

时间:2019-04-08 21:22:53      阅读:135      评论:0      收藏:0      [点我收藏+]

IDEA创建webservice项目 由于我本地下载不了远程的jar包,手动导入jar和配置依赖axis-1.4

axis-1.4.jar解压后添加到源文件 有一部分servlet需要依赖

技术分享图片

搜索了很多不知具体是那些jar我就导入了这些jar

技术分享图片

实现比较简单

1.定义接口 添加 @webservice 方法@webmethod

2.实现类

3.发布

Endpoint.publish(addrs,new testwebserviceImpl());发布制定的方法和发布地址
/**
 * Created by Administrator on 2019/4/3/0003.
 */
public class demo {
    public static void main(String[] args) {
        String addrs="http://localhost:8080/test/webservice";
        Endpoint.publish(addrs,new testwebserviceImpl());
        System.err.println("发布");
    }
}

 

package example;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 * Created by Administrator on 2019/4/3/0003.
 */
@WebService
public interface testwebservice {
    @WebMethod
    String sayhi(String name);
}
package example;

import javax.jws.WebService;

/**
 * Created by Administrator on 2019/4/3/0003.
 */
@WebService
public class testwebserviceImpl implements testwebservice {
    @Override
    public String sayhi(String name) {
        System.err.println("ok");
        String a=name+"-------------";
        return a;
    }
}

发布地址+?wsdl 为xml格式数据

http://localhost:8080/test/webservice?wsdl

技术分享图片

 

 新建一个webservice客户端

首先我们先创建一个client客户端的项目,然后我们通过Win+R组合键调出cmd,在cmd中输入wsimport -s 我们这个项目的src路径 -keep 我们发布的webservice地址 

 wsimport -s F:\clientwebs -keep http://localhost:8080/test/webservice?wsdl

解析xml生成实体类

技术分享图片

mian方法调用发布接口

package example;/**
 * Created by Administrator on 2019/4/3/0003.
 */
public class HelloWorld {
  public static void main(String[] args) {
      TestwebserviceImplService testwebserviceImplService = new TestwebserviceImplService();

      TestwebserviceImpl wsImpl = testwebserviceImplService.getTestwebserviceImplPort();

      String nmsl = wsImpl.sayhi("nmsl");

      System.err.println(nmsl);


  }
}

技术分享图片

源码:

链接:https://pan.baidu.com/s/16gRbYxH-AmKXQzsvnULZBg
提取码:rgyh

 

webservice的简单实现发布和客户端

原文:https://www.cnblogs.com/cuinima/p/10673396.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!