一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring boot webService使用方法解析

Spring boot webService使用方法解析

2020-09-22 00:22手撕高達的村長 Java教程

這篇文章主要介紹了Spring boot webService使用方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

以前一家公司,項目用到webservice,不過后來沒待多久,當時也要弄別的也就沒有研究,

這次也遇到過這樣一個使用場景,需要對接別人的一個人臉識別服務,在什么都沒有的情況下,對方只給了一個wsdl的地址過來,全程都靠自己去研究了.

先就webservice 講下自己的理解把,感覺有點像websockt ,它可以實現一個服務端, 然后在客戶端去調用服務端去完成服務端的操作.

這里使用spring-boot

1.先創(chuàng)建spring-boot項目,引入jar包

2.創(chuàng)建一個對象.

?
1
2
3
4
5
6
7
8
9
10
11
<!-- web Services -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>
 
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
      <version>3.2.7</version>
    </dependency>

創(chuàng)建一個服務端接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.sunzy.mywebservice;
 
 
import lombok.Getter;
import lombok.Setter;
 
/**auth : szy
 *time : 2020-01-03
 **/
@Getter
@Setter
public class Person {
  private Integer id;
  private String name;
  private String niceName;
  private Integer age;
  private Double height;
}

服務端的實現方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.sunzy.mywebservice.service.Impl;
 
import com.alibaba.fastjson.JSONArray;
import com.sunzy.mywebservice.Person;
import com.sunzy.mywebservice.service.TestApiService;
import org.springframework.stereotype.Component;
 
import javax.jws.WebService;
import java.util.List;
 
/**auth : szy
 *time : 2020-01-03
 **/
@Component
@WebService(name = "testApiService",
    targetNamespace = "http://service.mywebservice.sunzy.com",
    endpointInterface = "com.sunzy.mywebservice.service.TestApiService",
    portName = "10000")
public class TestApiServiceImpl implements TestApiService {
  @Override
  public Person insertPersonInfo(String person) {
    System.out.println("服務端接口到了請求:person="+person);
    List<Person> list = JSONArray.parseArray(person, Person.class);
    //TODO 邏輯處理
    return list.get(0);
  }
}

配置文件,將服務進行開放出去,給外部使用.

到這里已經完成了,運行項目.訪問地址:http://localhost:8080/ws/testApiService?wsdl

Spring boot webService使用方法解析

能輸出下面,表示服務端部署成功了.

那么下面是客戶端如何去訪問

可以新建一個項目,這里采用本項目去調用.用idea 去解析wsdl,生成對應的代碼.

選擇項目

Spring boot webService使用方法解析

通過wsdl,生成java代碼.

Spring boot webService使用方法解析

上面填生成的地址,下面試填寫包名,重點 這里上面的地址是要有效能訪問到的,不然程序是讀取不到東西的,更不要說解析了

生成代碼完成后,可以看到代碼:

Spring boot webService使用方法解析

調用方法,這里就自己寫一個控制器,模仿下客戶端去調用.

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.sunzy.mywebservice.controller;/**
 * @title: Hello
 * @projectName mywebservice
 * @description: TODO
 * @author :szy
 * @date 2020/1/3-15:44
 */
 
import com.sunzy.mywebservice.Person;
import com.sunzy.mywebservice.config.TestApiServiceImplService;
import com.sunzy.mywebservice.service.TestApiService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.net.URL;
import java.util.Map;
 
/**auth : szy
 *time : 2020-01-03
 **/
@RestController
@RequestMapping("/adminWebservice")
public class Hello {
 
  // 獲取單位信息
  @GetMapping(value="/sync")
  public String sync(@RequestParam(value="data") String data) throws Exception{
 
    // 接口地址
           String address = "http://localhost:8080/ws/testApiService?wsdl";
           // 代理工廠
           JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
           // 設置代理地址
           jaxWsProxyFactoryBean.setAddress(address);
           // 設置接口類型
           jaxWsProxyFactoryBean.setServiceClass(TestApiService.class);
           // 創(chuàng)建一個代理接口實現
          TestApiService us = (TestApiService) jaxWsProxyFactoryBean.create();
 
           // 數據準備
           String userId = "[{\"name\":\"JACK\"},{\"name\":\"TOM\"}]";
           // 調用代理接口的方法調用并返回結果
           Person person = us.insertPersonInfo(userId);
           System.out.println("返回結果:" + person.toString());
 
    return "index";
  }
 
 
 
  // 動態(tài)調用 外部調用
  @GetMapping(value="/dontest")
  public String dontest(@RequestParam(value="data") String data) throws Exception{
 
    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
    Client client = dcf.createClient("http://127.0.0.1:8080/ws/testApiService?wsdl");
 
 
    Object[] objects = new Object[0];
 
    try {
      // invoke("方法名",參數1,參數2,參數3....);
      // 數據準備
      String userId = "[{\"name\":\"JACK\"},{\"name\":\"TOM\"}]";
      objects = client.invoke("insertPersonInfo", userId);
 
      System.out.println("返回數據:" + objects[0]);
 
    } catch (java.lang.Exception e) {
      e.printStackTrace();
    }
 
    return "index";
  }
 
 
  // 動態(tài)調用 外部調用(外部模擬客服端調用服務端)
  @GetMapping(value="/dontest2")
  public String dontest2(@RequestParam(value="data") String data) throws Exception{
 
    //調用服務端
    TestApiServiceImplService serviceImplService = new TestApiServiceImplService();
 
    com.sunzy.mywebservice.config.TestApiService apiService = serviceImplService.get10000();
    String userId = "[{\"name\":\"小紅\"},{\"name\":\"小藍\"}]";
 
    com.sunzy.mywebservice.config.Person x = apiService.insertPersonInfo(userId);
 
    //服務端返回的數據
    System.out.println("返回數據:" + x.toString());
 
    return "index";
  }
}

通過postman可以看到調用成功.

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/sunxun/p/12171814.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产一级特黄aa大片免费 | 天天操精品视频 | 向日葵视频app下载18岁以下勿看 | 免费高清www动漫视频播放器 | 天海翼最新作品 | 果冻传媒第一二三专区 | 私人chinese beauty| 18videossex性欧美69 | 亚洲一区二区三区不卡在线播放 | 亚洲精品视频一区 | 久久综久久美利坚合众国 | 消息称老熟妇乱视频一区二区 | 四虎成人免费大片在线 | 欧美a级在线观看 | 成人网中文字幕色 | 亚洲欧美在线免费 | 女高h| 性xxxx中国老妇506070 | 免费在线观看小视频 | 国产网站免费在线观看 | 日韩精品欧美高清区 | 18日本人| 欧美精品久久久久久久影视 | 亚洲123区 | a级毛片毛片免费很很综合 a级黄色视屏 | 国产欧美一区视频在线观看 | 毛片区 | 无限好资源第一片免费韩国 | 日韩精品成人 | 国产一卡二卡四卡免费 | 国产人人草| 亚洲欧美专区 | 成人影院免费看 | 免费看片黄色 | 四虎成人网| 国产成人精品高清不卡在线 | 日韩在线 中文字幕 | ady成人映画网站官网 | 东京道一本热大交乱 | 国产日日操 | 精品久久久久久久久久久久久久久 |