首页 > 学院 > 开发设计 > 正文

在eclipse中使用jax-ws构建webservices服务端和客户端

2019-11-14 20:55:48
字体:
来源:转载
供稿:网友
在eclipse中使用jax-ws构建webservices服务端和客户端服务端:
package com.yinfu.service;import javax.jws.WebService;import javax.xml.ws.Endpoint;@WebService  public class TestWebsService {    public String sayHello(String username) {        return "Hello: " + username;    }        public static void main(String[] args) {        Endpoint.publish("http://localhost:8075/com.yinfu.service.TestWebsService", new TestWebsService());        System.out.PRintln("Success");    }}

在想要发布为WebService的类上加上注解@WebService,这个类的方法就变为WebService的方法了,再通过Endpoint的publish方法,发布这个服务,到此,一个最简单的WebService搞定。运行main方法,在浏览器里输入”http://localhost:8075/com.yinfu.service.TestWebsService?wsdl“会看到你的WSDL信息。

OK,说明服务端没有问题,搭建成功

客户端:

在命令行输入命令 wsimport -p [包名] -keep [发布的服务地址?wsdl] 生成客户端代码,如生成本例的客户端代码wsimport -p com.yinfu.service.client-keep http://localhost:8075/com.yinfu.service.TestWebsService?wsdl“,当然,前提是你已经配好了JAVA环境变量。控制台会显示

注意现在需要找到生成的客户端java代码,控制台指向在那生成的代码就在哪里,例如我控制台指向的是C:/Users/Administrator/那么我生成的java文件就在C:/Users/Administrator/com/yinfu/service/client/中,这个地方注意一下就可以了,有很多人不知道生成的代码去哪里了!

OK,把生成的代码拷贝到客户端的项目中,

package com.yinfu.service.client;public class HelloClient {    /**     * @param args     */    public static void main(String[] args) {        TestWebsServiceService myService = new TestWebsServiceService();        TestWebsService ms = myService.getTestWebsServicePort();        String s = ms.sayHello("why");        System.out.println(s);    }}

利用这些生成的客户端代码,就可以调用这个WebService服务了:执行代码,输出:Hello why。运行的时候要注意服务端的项目服务是启动的。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表