springboot使用restTemplate调用webservice接口

10 篇文章 0 订阅
2 篇文章 0 订阅

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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值