使用RestTemplate發送multipart/form-data格式的數據
阿新 • • 發佈:2017-11-01
return entity 格式 protect 數據格式 () tco log ...
現有業務場景需要使用RestTemplate發送一個post請求,請求格式為multipart/form-data的,可以使用一下方法
public Object sendRequest(Object obj) { RestTemplate restTemplate = new RestTemplate(); //設置請求頭 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>( popHeaders(obj), headers); //發送請求,設置請求返回數據格式為String ResponseEntity<String> responseEntity = restTemplate.postForEntity("http:xxx.xx", request, String.class); } //組裝請求體 protected MultiValueMap<String, String> popHeaders(Object obj) { SubmitOrderDTO submit = (SubmitOrderDTO) obj; MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); map.add("order_id", submit.getOrderId()); map.add("userName",submit.getUserName()); //..... return map; }
使用RestTemplate發送multipart/form-data格式的數據