1. 程式人生 > >RestTemplate post提交方式的一些總結

RestTemplate post提交方式的一些總結

1在這裡插入圖片描述

測試取值
@PostMapping(value = “test1”)
public MapiResult test1(HttpServletRequest request){
//可以用實體去接收 UserVo new HttpEntity<>(userVo,httpHeaders) 不可以獲取到值
//new HttpEntity<>(map,httpHeaders); 提交可以獲取到值
String ss= request.getParameter(“code”);
System.out.println(ss);
return null;
}
@PostMapping(value = “test2”)
public MapiResult test2(@RequestBody UserVo userVo){
// new HttpEntity<>(userVo,httpHeaders) 提交方式 可以獲取到值
// new HttpEntity<>(map,httpHeaders); 報錯 報錯 Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported
System.out.println(userVo.getCode());
return null;

}

呼叫程式碼
@Override
@BackendApi(“繫結手機號”)
public String bindMobile(UserVo userVo) {
String destUrl=mps+ RestfulUrlConst.USER_ACCOUNT_BINDMOBILE;
String resp;
HttpHeaders httpHeaders =new HttpHeaders();
httpHeaders.set(“Login-Ticket”, userVo.getLoginTicket());
MultiValueMap<String, Object> map= RestTemplateUtil.proMultiValueMap(JSONObject.parseObject(JSONObject.toJSONString(userVo)));
HttpEntity<MultiValueMap<String,Object>> httpEntity = new HttpEntity<>(map,httpHeaders);
HttpEntity httpEntity22 = new HttpEntity<>(userVo,httpHeaders);
resp= restTemplate.exchange(“

http://localhost:9000/api/v1/user/test”, HttpMethod.POST, new HttpEntity<>(userVo,httpHeaders), String.class).getBody();
resp= restTemplate.exchange(“http://localhost:9000/api/v1/user/test”, HttpMethod.POST, httpEntity, String.class).getBody();
LoggerUtil.logger().info(“繫結手機號,呼叫後臺介面返回物件{}”,resp);
return resp;
}

總結:
resp= restTemplate.exchange(“http://localhost:9000/api/v1/user/test”, HttpMethod.POST, new HttpEntity<>(userVo,httpHeaders), String.class).getBody();
對應前端 接收 test2(@RequestBody UserVo userVo) 比較常用 寫法簡單 不用轉map