加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService - 使用JDK(jetty)生成Stub进行开发

发布时间:2020-12-16 21:42:16 所属栏目:安全 来源:网络整理
导读:本篇简要记录使用jdk生成Stub进行service与client的测试,未与spring结合。这里使用的是CXF。 【1】配置系统环境变量 如果使用jetty生成Stub来编写客户端项目,需要进行如下配置: 【2】编辑Service并进行发布 ① 编写service接口 package com.web.service;

本篇简要记录使用jdk生成Stub进行service与client的测试,未与spring结合。这里使用的是CXF。

【1】配置系统环境变量

如果使用jetty生成Stub来编写客户端项目,需要进行如下配置:

这里写图片描述


【2】编辑Service并进行发布

① 编写service接口

package com.web.service;

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

@WebService
public interface ServiceServer {

    @WebMethod
    public String sayHello(String name);

}

② 编写其实现类

package com.web.service.impl;

import javax.jws.WebService;

import com.web.service.ServiceServer;

@WebService
public class ServiceServerImpl implements ServiceServer{

    @Override
    public String sayHello(String name) {
        System.out.println("server sayHello "+name);
        return "hello"+name;
    }

}

③ 使用main方法进行发布

package com.web.service.impl;

import java.util.Date;

import javax.xml.ws.Endpoint;

public class ServerTest {

    public static void main(String[] args) {

        String address = "http://192.168.2.225:8989/ServiceServerImpl";
        Endpoint.publish(address,new ServiceServerImpl());
        System.out.println("webservice 发布成功!"+new Date());
    }
}

这里写图片描述


【3】根据address,查看并保存wsdl文件

① 浏览器中输入地址,查看wsdl

http://192.168.2.225:8989/ServiceServerImpl?wsdl

这里写图片描述


将其保存为test.wsdl放入客户端项目下(新建一个客户端项目)

这里写图片描述

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.web.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://service.web.com/" name="ServiceServerImplService" targetNamespace="http://impl.service.web.com/">
  <wsdl:import location="http://192.168.2.225:8989/ServiceServerImpl?wsdl=ServiceServer.wsdl" namespace="http://service.web.com/">
    </wsdl:import>
  <wsdl:binding name="ServiceServerImplServiceSoapBinding" type="ns1:ServiceServer">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="sayHello">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="sayHello">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="sayHelloResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="ServiceServerImplService">
    <wsdl:port binding="tns:ServiceServerImplServiceSoapBinding" name="ServiceServerImplPort">
      <soap:address location="http://192.168.2.225:8989/ServiceServerImpl"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

【4】根据wsdl生成Stub,进行客户端测试

① DOS进入客户端项目src路径

这里写图片描述


② 根据项目下的wsdl生成Stub

wsimport -keey url

这里写图片描述


③ 刷新项目,可以看到src下生成了service和impl

这里写图片描述


另外,还可以直接根据wsdl的url地址生成Stub,不用再保存wsdl具体文件到项目下。

这里写图片描述


上面两种方式都是使用jdk生成Stub,还可以使用如下命令使用jetty进行生成:

wsdl2java url

④ 编写main方法,进行客户端调用

package com.web.client;

import com.web.service.impl.ServiceServer;
import com.web.service.impl.ServiceServerImplService;

public class Client {

    public static void main(String[] args) {
        ServiceServerImplService factory = new ServiceServerImplService();
        ServiceServer serviceServerImplPort = factory.getServiceServerImplPort();
        System.out.println(serviceServerImplPort.getClass());
        String result = serviceServerImplPort.sayHello("Tom");
        System.out.println("Client "+result);
    }
}

客户端输出结果如下:

这里写图片描述


服务端输出结果如下:

这里写图片描述


项目源码与jar下载:http://download.csdn.net/download/j080624/10051042

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读