0


springboot使用restTemplate调用webservice接口

1.首先确定wsdl的http地址,使用postman测试接口是否成功

在浏览器输入webservice地址可以找到相应的请求和响应示例。

如果postman返回了正确的数据,就说明测试成功!

2.接下来代码:

package com.test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.json.XML;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
/**
* @author "qs"
*/
public class webTest{

    @Autowired
    private RestTemplate restTemplate;

    public void getData() {
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("text/xml;charset=UTF-8");
        // 设置请求头对象contentTyp的为text/xml;charset=UTF-8
        headers.setContentType(type);
        StringBuffer soapRequestData = new StringBuffer("");
        soapRequestData.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
        soapRequestData.append("<soap:Body>");
        soapRequestData.append("<GetData xmlns=\"http://localhost/\" />");
        soapRequestData.append("</soap:Body>");
        soapRequestData.append("</soap:Envelope>");
        String url = "你的地址";
        String responseStr = exchangeForEntity(headers, soapRequestData + "", url, HttpMethod.POST, null);
        String tmpStr = StringEscapeUtils.unescapeXml(responseStr);
        if (StringUtils.isNotBlank(tmpStr)) {
            org.json.JSONObject xmlJSONObj = null;
            try {
                xmlJSONObj = XML.toJSONObject(tmpStr);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (xmlJSONObj != null) {
               //处理数据
               System.out.println(JSON.toJSONString(xmlJSONObj));
            }
        }
    }

    /**
     * 执行http请求
     *
     * @param headers
     * @param params
     * @param url
     * @param method
     * @param mediaType
     * @param <T>       MultiValueMap<String, String>  JSONObject  Map
     * @return
     */
    public <T> String exchangeForEntity(HttpHeaders headers, T params, String url, HttpMethod method, MediaType mediaType) {
        //设置请求头
        if (mediaType != null) {
            headers.setContentType(mediaType);
        }
        HttpEntity<T> requestEntity = new HttpEntity<>(params, headers);
        //  执行HTTP请求
        ResponseEntity<String> response = restTemplate.exchange(url, method, requestEntity, String.class);
        return response.getBody();
    }
}
标签: postman 测试工具

本文转载自: https://blog.csdn.net/qq_38464466/article/details/129398720
版权归原作者 奥斯塔拉夫斯基 所有, 如有侵权,请联系我们删除。

“springboot使用restTemplate调用webservice接口”的评论:

还没有评论