使用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<>();
相關文章
相關標籤/搜索