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命令很容易填错

  • 相关阅读:
    hdu 5119 Happy Matt Friends
    hdu 5128 The E-pang Palace
    hdu 5131 Song Jiang's rank list
    hdu 5135 Little Zu Chongzhi's Triangles
    hdu 5137 How Many Maos Does the Guanxi Worth
    hdu 5122 K.Bro Sorting
    Human Gene Functions
    Palindrome(最长公共子序列)
    A Simple problem
    Alignment ( 最长上升(下降)子序列 )
  • 原文地址:https://www.cnblogs.com/xiang--liu/p/11066317.html
Copyright © 2011-2022 走看看