zoukankan      html  css  js  c++  java
  • WebService服务发布与使用(JDK自带WebService)

    简单粗暴,直接上步骤
    一、先建立一个web项目,名字叫MyService
    这里写图片描述
    名字为MyService

    新建Java

    package com.webService;
    import javax.jws.WebService;//别倒错包哦
    import javax.xml.ws.Endpoint;//别倒错包哦
    
    
    @WebService//注解别忘了
    public class ServiceTest {
    
        public String getMessage(String name) {
            return name+"你过来一下";
        }
    
    
        public static void main(String[] args) {
            Endpoint.publish("http://localhost:8080/MyService/ServiceTest", new ServiceTest());//发布服务
            System.out.println("ServiceTest已启动");
        }
    }

    运行main方法
    这里写图片描述

    说明服务已经启动
    访问http://localhost:8080/MyService/ServiceTest?wsdl可以看到
    这里写图片描述

    说明发布成功了

    二、生成客户端
    再新建一个web项目,名字叫MyClient
    在src下建立com.client包

    win+R cmd打开windows命令窗口

    输入

    wsimport -s I:\eclipse_jee\workspaces\MyClient\src -p com.webClient -keep http://localhost:8080/MyService/ServiceTest?wsdl
     
    wsimport -s "C:\Users\user\AppData\Local\MyEclipse 2016 CI\MyEclipse 2016 CI\MyClient\src" -p com.webClient -keep http://localhost:8080/MyService/ServiceTest?wsdl


    wsimport -s "C:\Users\user\AppData\Local\MyEclipse 2016 CI\MyEclipse 2016 CI\MyClient\src" -p com.webClient -keep http://192.168.9.5:8080/MyService/ServiceTest?wsdl
     
    有空格用“”整个包起来
     

    就可以看到
    路径很重要

    I:eclipse_jeeworkspacesMyClientsrc 客户端项目所在目录
    com.webClient 包名
    http://localhost:8080/MyService/ServiceTest?wsdl wsdl地址

    然后refresh MyClient项目,生成类出现了
    这里写图片描述

    在src下建立test包,再建一个测试类ClientTest,代码如下

    package test;
    
    import com.webClient.ServiceTest;
    import com.webClient.ServiceTestService;
    
    public class ClientTest {
    
        public static void main(String[] args) {
            ServiceTest serviceTest = new ServiceTestService().getServiceTestPort();//初始化对象
            String name = serviceTest.getMessage("那个谁");//调用服务端方法
    
            System.out.println(name);//打印返回结果
        }
    }

    运行main方法

    这里写图片描述

    完美!!

    注意事项:
    1、jdk1.7及以上
    2、cmd命令很容易填错

  • 相关阅读:
    Python修饰符实践
    回文
    Linux下安装Qt
    Linux下安装PyQT
    Python闭包实践
    杂乱
    windows下脚本转到linux下,文件保存格式要转换
    lua table.sort的bug
    shell截取某段
    coredump
  • 原文地址:https://www.cnblogs.com/xiang--liu/p/11066317.html
Copyright © 2011-2022 走看看