Spring 之 RestTemplate 发送 HTTP请求
程序员文章站
2022-06-25 20:15:37
...
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
//设置请求头
HttpHeaders headers = new HttpHeaders();
//设置参数
LinkedMultiValueMap<String, Object> body = new LinkedMultiValueMap();
body.add("alertLevel", alertLevel);
body.add("alertType", alertType);
body.add("alertMessage", alertMessage);
body.add("alertMessageType", alertMessageType);
body.add("service", service);
body.add("data", data);
HttpEntity<LinkedMultiValueMap<String, Object>> entity = new HttpEntity(body, headers);
//请求接口,并接收返回的信息
String s = restTemplate.exchange(alertUrl, HttpMethod.POST, entity, String.class).getBody();
上一篇: 是否我真的一无所有