随笔 - 597  文章 - 4  评论 - 445  阅读 - 424万

基于Springboot整合RestTemplate调用Webservice接口

1、基于Springboot整合RestTemplate调用Webservice接口,如果感觉使用webservice客户端调用服务器端不会,或者不方便 的时候,可以尝试使用RestTemplate来调用Webservice接口

  首先,需要做的就是要获取到请求webservice服务器端的xml文件,此时,需要根据wsdl生成请求webservice服务器端的xml文件,可以使用SoapUi这个文件来操作,点击File -> New SOAP Project,来创建一个工程。

然后导入wsdl文件,起个工程名称,这个时候,导入成功之后,就可以看到要请求的xml格式了。

此时,就可以看到要请求的xml,如果有需要进行验证的参数封装到请求头里面soapenv:Header。

将需要验证的参数封装到请求头里面,如下所示:

 

 

2、当你拿到要请求的参数的时候,此时,我想使用resttemplate,还是其他请求http的工具,你都可以进行服务调用的吧,关键点,就是你拼装请求参数,就可以了。

复制代码
 1 @Override
 2 public String getXxxRest(UserInfo userInfo) {
 3     // 创建一个请求头对象
 4     HttpHeaders headers = new HttpHeaders();
 5     MediaType type = MediaType.parseMediaType("text/xml;charset=UTF-8");
 6     // 设置请求头对象contentTyp的为text/xml;charset=UTF-8
 7     headers.setContentType(type);
 8 
 9 
10     String username = userInfo.getUsername();
11     String password = userInfo.getPassword();
12     String inputXml = userInfo.getinputXml();
13     
14     // 将请求参数进行封装并进行远程接口服务调用
15     // 构造webservice请求参数
16     StringBuffer soapRequestData = new StringBuffer("");
17     soapRequestData.append(
18             "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.tongtech.com/\">");
19     soapRequestData.append("<soapenv:Header>");
20     soapRequestData.append("<tongtechheader>");
21     soapRequestData.append("<id>");
22     soapRequestData.append(userInfo.getId());
23     soapRequestData.append("</id>");
24 
25     soapRequestData.append("<cart>");
26     soapRequestData.append(userInfo.getCart());
27     soapRequestData.append("</cart>");
28 
29     soapRequestData.append("</tongtechheader>");
30     soapRequestData.append("</soapenv:Header>");
31 
32     soapRequestData.append("<soapenv:Body>");
33     soapRequestData.append("<web:getBirthInfo>");
34 
35     soapRequestData.append("<username>");
36     soapRequestData.append(username);
37     soapRequestData.append("</username>");
38 
39     soapRequestData.append("<password>");
40     soapRequestData.append(password);
41     soapRequestData.append("</password>");
42 
43     soapRequestData.append("<inputXml>");
44     soapRequestData.append(userInfo.getInputXml());
45     soapRequestData.append("</inputXml>");
46 
47     soapRequestData.append("</web:getBirthInfo>");
48     soapRequestData.append("</soapenv:Body>");
49     soapRequestData.append("</soapenv:Envelope>");
50 
51     // 创建请求
52     HttpEntity<String> request = new HttpEntity<String>(soapRequestData + "", headers);
53 
54     // 发送post请求并获取到响应结果
55     String str = null;
56     // 封装一下返回结果
57     Map<String, Object> analyseResult = new HashMap<String, Object>();
58     try {
59         str = restTemplate.postForObject(请求地址, request, String.class);
60         logger.info("-----------Response content-----------: " + str);
61     } catch (RestClientException e) {
62         e.printStackTrace();
63     }
64 
65     return str;
66 }
复制代码

虽然你可以使用Webservice客户端来调用服务器端,但是如果实在不想那样搞,也可以通过resttemplate或者其他http请求方式进行接口i调用的。

posted on   别先生  阅读(4662)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

点击右上角即可分享
微信分享提示