基于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 2021-06-03 21:57  别先生  阅读(4325)  评论(0编辑  收藏  举报