WebService(1)——使用JDK开发WebService

服务器端开发

  • 新建一个java工程,写一个接口,加上注解,@WebService和@WebMethod
package com.ws.service;

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

@WebService
public interface HelloWS {
    @WebMethod
    String sayHello(String name);
}
  • 实现类也要加上@WebService注解
package com.ws.service;

import javax.jws.WebService;

@WebService
public class HelloWSImpl implements HelloWS {

    @Override
    public String sayHello(String name) {
        return "hello "+name;
    }

}
  • 写一个main方法,使用Endpoint来发布webservice
package com.ws.main;

import javax.xml.ws.Endpoint;

import com.ws.service.HelloWSImpl;

public class Main {

    public static void main(String[] args) {
        //必须写本地IP
        String address = "http://172.17.202.155:9999/ws01/hellows";
        Endpoint.publish(address , new HelloWSImpl());
        System.out.println("发布成功");

    }

}

这里写图片描述

运行main之后,可以看到上图箭头的位置并没有变灰,说明程序还在运行,它正在等待被访问

  • 用浏览器查看wsdl文档,路径是http://172.17.202.155:9999/ws01/hellows?wsdl
    这里写图片描述

  • 在eclipse工具栏有一个webservice浏览器,我们使用它来测试一下
    这里写图片描述

  • 打开webservice浏览器后,按下图步骤,输入url,点击go
    这里写图片描述

  • 然后可以测试我们发布的接口了
    这里写图片描述

客户端开发

  • 新建一个java工程作为客户端
  • 打开一个cmd命令行
    这里写图片描述

  • 在命令行中进入客户端项目的src所在目录,我们需要在这里使用命令生成客户端代码,命令是:wsimport -keep url。url是我们发布的webservice的url加上?wsdl,回车,自动生成代码

  • 在eclipse中刷新客户端项目,就能看到自动生成的代码了
    这里写图片描述

  • 写一个main调用webservice

package com.ws.service.client.test;

import com.ws.service.HelloWSImpl;
import com.ws.service.HelloWSImplService;

public class Main {

    public static void main(String[] args) {
        // 调用webservice
        HelloWSImplService factory = new HelloWSImplService();
        HelloWSImpl helloWS = factory.getHelloWSImplPort();
        System.out.println(helloWS.sayHello("tom"));
    }

}

客户端开发完毕

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值