Spring boot webService使用方法解析

2025-05-29 0 91

以前一家公司,项目用到webservice,不过后来没待多久,当时也要弄别的也就没有研究,

这次也遇到过这样一个使用场景,需要对接别人的一个人脸识别服务,在什么都没有的情况下,对方只给了一个wsdl的地址过来,全程都靠自己去研究了.

先就webservice 讲下自己的理解把,感觉有点像websockt ,它可以实现一个服务端, 然后在客户端去调用服务端去完成服务端的操作.

这里使用spring-boot

1.先创建spring-boot项目,引入jar包

2.创建一个对象.

?

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>

创建一个服务端接口

?

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);

// 创建一个代理接口实现

TestApiService us = (TestApiService) jaxWsProxyFactoryBean.create();

// 数据准备

String userId = "[{\\"name\\":\\"JACK\\"},{\\"name\\":\\"TOM\\"}]";

// 调用代理接口的方法调用并返回结果

Person person = us.insertPersonInfo(userId);

System.out.println("返回结果:" + person.toString());

return "index";

}

// 动态调用 外部调用

@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";

}

// 动态调用 外部调用(外部模拟客服端调用服务端)

@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可以看到调用成功.

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Spring boot webService使用方法解析 https://www.kuaiidc.com/116880.html

相关文章

发表评论
暂无评论