RestTemplate联调接口写法

--返回的是json串
{
HttpHeaders headers = new HttpHeaders();

MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("params", psrRequestBody);
HttpEntity<MultiValueMap<String, String>> formEntity = new HttpEntity<>(params, headers);
ResponseEntity responseEntity=restTemplate.postForEntity(               hyDoaminUrl+"/PSRCenter/queryServices/PWLinePSR",formEntity, String.class);    --返回的结构体
String result = responseEntity.getBody().toString();  --getBody()返回的具体对象
ResponseVO responseVO=JSONObject.parseObject(result, ResponseVO.class);
List<HyPWLinePSR> pwLinePSRList =  JSONObject.parseArray(responseVO.getResult().toString(), HyPWLinePSR.class);
System.out.println(pwLinePSRList);
}

--返回的是对象
{
HttpHeaders headers = new HttpHeaders();
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("params", psrRequestBody);
HttpEntity<MultiValueMap<String, String>> formEntity = new HttpEntity<>(params, headers);
ResponseEntity responseEntity=restTemplate.postForEntity(            hyDoaminUrl+"/PSRCenter/queryServices/PWLinePSR",formEntity, String.class);
String result = responseEntity.getBody().toString();
ResponseVO responseVO=JSONObject.parseObject(result, ResponseVO.class);
List<DataModel> dataModelList = new ArrayList<>();   dataModelList.addAll(JSONObject.parseArray(JSONObject.toJSONString(responseVO.getResult()), DataModel.class));
System.out.println(dataModelList);
}

你可能感兴趣的:(Spring,boot)