SpringBoot restTemplate请求get和post

一、restTemplate-get

public LunkrResult getAvatar(String uid) throws JsonProcessingException {
        //设置headers
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.valueOf("text/x-json;charset=UTF-8"));
        List cookies = new ArrayList<String>();
        cookies.add("cookie=" + cookie);
        headers.put(HttpHeaders.COOKIE, cookies);
        HttpEntity<String> request = new HttpEntity(null, headers);
        //url参数
        Map<String, Object> urlParamter = new HashMap<>();
        urlParamter.put("uid", uid);
        LunkrResult result = getURIRequest("cim.common:getXXX", request, urlParamter);
        return result;
    }


private Map<String,Object> getURIRequest(String func, HttpEntity body
            , Map<String, Object> urlParamter) throws JsonProcessingException {
        String url = properties.getWebname() + "/s/json?func=" + func;
        for (String key : urlParamter.keySet()) {
            url += "&" + key + "=" + urlParamter.get(key).toString();
        }
        //encode下url
        URI encodeUrl = UriComponentsBuilder.fromHttpUrl(url).build().encode().toUri();
        //ResponseEntity response = restTemplate.getForEntity(encodeUrl, body, String.class);
        ResponseEntity<byte[]> response = restTemplate.exchange(encodeUrl, HttpMethod.GET, body, byte[].class);
        Map<String,Object> result = new HashMap();
        if (response.getStatusCode() == HttpStatus.OK) {
            result.put("body", response.getBody());
            result.put("code", "OK");
        } else {
            ResponseEntity newresponse = restTemplate.exchange(encodeUrl, HttpMethod.GET, body, String.class);
            result = new Gson().fromJson((String) newresponse.getBody(), Map.class);
        }
        return result;
    }

二、restTemplate-post

public void prepare() {
    //设置headers
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.valueOf("text/x-json;charset=UTF-8"));
    List cookies = new ArrayList<String>();
    cookies.add("cookie=" + cookie);
    headers.put(HttpHeaders.COOKIE, cookies);

    //设置post请求body
    Map<String, Object> prepareBody = new HashMap<>();
    prepareBody.put("att", 0);
	prepareBody.put("fileName", "文件名");
	String prepareJson = new ObjectMapper().writeValueAsString(prepareBody);
	HttpEntity<String> request = new HttpEntity(prepareJson, headers);
	Map<String, Object> urlParamter = new HashMap<>();
	urlParamter.put("uid", fileUid);
	LunkrResult prepareResult = postURIRequest("test:prepare", request, urlParamter);
}

private Map<String,Object> postURIRequest(String func, HttpEntity body
            , Map<String, Object> urlParamter) throws JsonProcessingException {
        String url = properties.getWebname() + "/s/json?func=" + func;
        for (String key : urlParamter.keySet()) {
            url += "&" + key + "=" + urlParamter.get(key).toString();
        }
        if (!StringUtils.isEmpty(accessToken.getLunkrSid())) {
            url += "&sid=" + accessToken.getLunkrSid();
        }
        URI encodeUrl = UriComponentsBuilder.fromHttpUrl(url).build().encode().toUri();
        ResponseEntity response = restTemplate.postForEntity(encodeUrl, body, String.class);
        //restTemplate.exchange(encodeUrl, HttpMethod.POST, body, String.class);
        Map<String,Object> result = new Gson().fromJson((String) response.getBody(), Map.class);
        return result;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Venlenter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值