resttemplate使用

RestTemplate String url = "http://aaa.com"; URI uri = URI.create(url);
post请求无参数 JSONObject jsonObject = restTemplate.postForObject(uri, "{}", JSONObject.class); if(jsonObject!=null){return jsonObject.toJavaObject(A.class)}
post请求有参数 JSONObject jsonObject = restTemplate.postForObject(uri, addWarningRequest, JSONObject.class);
post请求有参数带请求头 HttpHeaders headers = new HttpHeaders();
headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
headers.set(apiUrl.getKeyName(), apiUrl.getKeyValue());
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<String> httpEntity = new HttpEntity<>(s, headers);
JSONObject jsonObject = restTemplate.postForObject(uri, httpEntity, JSONObject.class);
get请求有参数带请求头 String url = “”;
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
String urlParam = builder.queryParam("date", date).build().encode().toString();
HttpHeaders headers = getCommonHeader();
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(headers);
ResponseEntity<String> result = restTemplate.exchange(urlParam, HttpMethod.GET, httpEntity, String.class);
if (result!=null&&HttpStatus.OK.equals(String.valueOf(result.getStatusCodeValue()))) {
String str = result.getBody();
if (!StringUtils.isEmpty(str)) {
JSONArray array = JSON.parseArray(str);
for (int i = 0; i < array.size(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
GetWarningMessageResponse getWarningMessageResponse = jsonObject.toJavaObject(GetWarningMessageResponse.class);
list.add(getWarningMessageResponse);
}
}
return list;

resttemplate使用

上一篇:DNS域名解析之正向解析


下一篇:Laravel 集成 laravel Scout + Elasticsearch