以前一家公司,项目用到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
能输出下面,表示服务端部署成功了.
那么下面是客户端如何去访问
可以新建一个项目,这里采用本项目去调用.用idea 去解析wsdl,生成对应的代码.
选择项目
通过wsdl,生成java代码.
上面填生成的地址,下面试填写包名,重点 这里上面的地址是要有效能访问到的,不然程序是读取不到东西的,更不要说解析了
生成代码完成后,可以看到代码:
调用方法,这里就自己写一个控制器,模仿下客户端去调用.
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
相关文章
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
-
2025-05-27 59
-
2025-05-29 34
-
2025-05-29 38
-
详解使用Mybatis-plus + velocity模板生成自定义的代码
2025-05-29 79 -
2025-05-29 54