1. 程式人生 > >SpringBoot restTemplate請求get和post

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