使用restTemplate发送post请求,传入参数是在requestBody请求体中,以json形式传输

@PostMapping
public ResponseResult add(User user){
HttpHeaders httpHeaders = new HttpHeaders();
MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8");
httpHeaders.setContentType(type);

// MultiValueMap<String, Object> map=new LinkedMultiValueMap<>();

HashMap<String, Object> map = new HashMap<>();
map.put("userName",user.getUserName());
map.put("passWord",user.getPassWord());
HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(map,httpHeaders);
ResponseEntity<ResponseResult> responseResultResponseEntity = restTemplate.postForEntity(GLOBAL_URL, objectHttpEntity, ResponseResult.class);
return responseResultResponseEntity.getBody();


特别注意,当没有请求头信息时,用 MultiValueMap<String, Object> map=new LinkedMultiValueMap<>();
当有请求头信息时,用 HashMap<String, Object> map = new HashMap<>();

猜你喜欢

转载自www.cnblogs.com/LX51/p/12214220.html