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;
相关文章
- 01-16STM32F407移植contiki2.6后使用LWIP库实现tcp client
- 01-16python – Cython:对于类型化的内存视图,我应该使用np.float_t而不是double
- 01-16使用 openocd 调试 STM32F103
- 01-16tk.mybatis使用方法
- 01-16使用TK框架中selectByPrimaryKey
- 01-16pagehelper分页+tk.mybatis的使用
- 01-16tk.mybatis.mapper使用
- 01-16python – 允许用户使用SQLAlchemy从db审计跟踪回滚
- 01-16[记录]Python2.7使用argparse模块
- 01-16python-使用通过TKinter在对象内部定义的图像按钮