webService學習(二)—— 呼叫自定義物件引數
webService學習(二)—— 呼叫自定義物件引數
本文主要內容:
1、如何通過idea進行webService Client的簡單實現(不再使用wsimport的方式,其實是ide幫我們做了)
2、在webservice中呼叫自定義引數(自定義類)。
好,下面上貨。
一、在idea的幫助下使用webService Client。
首先需要感謝一下文章:
https://my.oschina.net/nba/blog/482117
通過這篇文章能夠對在自己的專案中成功的引入webService。
簡單說一共兩步:
1、在專案中建立好自己的包(用來存放webservice生成的檔案)

2、選擇webservice方式,並且生成。

點選ok後,能夠在com.otherWebService包下生成對應的webservice client端的檔案。

二、在webservice中呼叫自定義引數(自定義類)
1、首先定義一個學生類:
package com.xueyoucto.xueyou;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
/**
* Created by Administrator on 2017/1/16.
*/
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
}
2、定義一個webService
package com.xueyoucto.xueyou;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
/**
* Created by Administrator on 2017/1/14.
*/
@WebService
public class HelloService {
public String testService(String params){
return "hello " + params;
}
public String testStudent(Student student){
return student.getName() + " : " + student.getAge();
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:8888/HelloService/",new HelloService());
System.out.println("ok");
}
}
3、釋出webService


4、按照上面介紹的匯入webservice Client的方式把HelloService匯入到專案中
5、嘗試呼叫webService
package com.firstServiceClient;
import com.otherWebService.HelloService;
import com.otherWebService.HelloServiceService;
import com.otherWebService.Student;
import javax.xml.ws.WebServiceException;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) {
System.out.println( "Hello World!" );
try {
HelloService helloService = new HelloServiceService().getHelloServicePort();
String s = helloService.testService("aabbcc");
System.out.println(s);
Student student = new Student();
student.setAge(13);
student.setName("aabbcc");
String s2 = helloService.testStudent(student);
System.out.println(s2);
}catch (WebServiceException wse){
System.out.println("服務未啟動");
wse.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
}
6、執行結果:

7、注意:
在webService的呼叫過程中,不能使用student的全建構函式。在網上查詢原因的時候遇到了一些解釋。主要原因就是在呼叫建構函式的時候,在本地其實是Student的一個代理類,並且這時候沒有和伺服器進行通訊。只有當webservice方法被呼叫的時候,才會和伺服器端通訊。
更詳細的請參考:
http://www.cnblogs.com/gisflyer/archive/2010/12/29/1919420.html
相關文章
- Vue學習(二)自定義指令Vue
- 引數匹配模型——Python學習之引數(二)模型Python
- SpringBoot基礎學習(三) 自定義配置、隨機數設定及引數間引用Spring Boot隨機
- vue17自定義指令(有引數,無引數)Vue
- 單據型別引數設定增加自定義引數並透過BOS標準函式呼叫型別函式
- SpringBoot自定義控制層引數解析Spring Boot
- webservice介面呼叫Web
- Laravel 授權系統 - 自定義引數Laravel
- Spring自定義引數解析器設計Spring
- Angular學習(4)自定義指令Angular
- webapi建立和呼叫WebServiceWebAPI
- element-UI change 事件傳遞自定義引數UI事件
- layUI Table自定義工具欄和搜尋引數UI
- 短視訊開發app,webservice自定義加入攔截器APPWeb
- 自定義物件池實踐物件
- Cordova學習--iOS自定義外掛iOS
- 【django學習-24】自定義外掛Django
- 一種WebService的呼叫方式Web
- 給 zsh 自定義命令新增引數自動補全
- 系統學習機器學習之半引數方法(二)--基於密度機器學習
- 使用自定義委託來呼叫Lua中的多返回值和長引數型別函式型別函式
- Lua學習(二)物件導向物件
- 【動手學深度學習】第五章筆記:層與塊、引數管理、自定義層、讀寫檔案、GPU深度學習筆記GPU
- 自定義鍵盤(二)
- 自定義事件相容處理物件事件物件
- 巧用GenericObjectPool建立自定義物件池Object物件
- Service或自定義物件使用LiveData物件LiveData
- 學 習 數 學 的 意 義
- IDEA自定義類註釋和方法註釋(自定義groovyScript方法實現多行引數註釋)Idea
- Flask-restful 用法及自定義引數錯誤資訊FlaskREST
- 升級後欄位引數有自定義函式失效函式
- 18-神經網路-自定義帶引數的層神經網路
- 騰訊WebService Api 跨域呼叫WebAPI跨域
- java webService 零基礎學習JavaWeb
- android自定義view(自定義數字鍵盤)AndroidView
- C# .net framework .net core 3.1 請求引數校驗, DataAnnotations, 自定義引數校驗C#Framework
- 引數匹配順序——Python學習之引數(三)Python
- Qt中多個原始檔中呼叫同一引數或物件等QT物件
- 物件呼叫動態變數物件變數