前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于Springboot整合RestTemplate调用Webservice接口

基于Springboot整合RestTemplate调用Webservice接口

作者头像
别先生
发布2021-06-10 09:36:21
3.2K0
发布2021-06-10 09:36:21
举报
文章被收录于专栏:别先生

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的工具,你都可以进行服务调用的吧,关键点,就是你拼装请求参数,就可以了。

代码语言:javascript
复制
 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调用的。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-06-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档