码迷,mamicode.com
首页 > 其他好文 > 详细

resttemplate使用

时间:2021-06-30 18:30:07      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:warning   gen   pen   iat   tpm   ret   sem   apple   creat   

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使用

标签:warning   gen   pen   iat   tpm   ret   sem   apple   creat   

原文地址:https://www.cnblogs.com/hai-/p/14953955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!