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已啟動"); } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

運行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
  • 1
  • 1

就可以看到
路徑很重要

I:\eclipse_jee\workspaces\MyClient\src 客戶端項目所在目錄
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);//打印返回結果 } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

運行main方法

這里寫圖片描述

完美!!

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM