欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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();
相关标签: springboot Java