通过使用spring中的RestTemplate发送Http POST请求同时设置请求头,请求体,然后将返回的json数据解析成java对象
//首先创建请求头
HttpHeaders requestHeaders = new HttpHeaders();
//根据需要设置请求头参数,可以设置多个
requestHeaders.add("key1","value1");
requestHeaders.add("key2","value2");
//创建请求体对象
MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
//设置请求体参数
requestBody.add("key1","value1");
//封装entity
HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(requestBody, requestHeaders);
//发送http请求 restTemplate提前注入
url = "http://127.0.0.1:8080/website" //请求的url地址
//接收返回entity
ResponseEntity<String> entity = restTemplate.exchange(url, HttpMethod.POST,requestEntity, String.class);
//获取json字符串body
String strJson = entity.getBody();
//用各种json工具解析为目标对象,这里是fastjson中的JSONObject
Target target = JSONObject.parseObject(strJson,Target.class)