将GET、PUT、POST 和 DELETE 四种请求整体来看,参数分为 3 中:
- 直接在url中的参数
- requestBody 中
- File类型
第1类 直接在url中的参数(Get和 delete 最常见)
个人觉得分为两种:
- 路径上的参数
- ? 号后面的参数(最常见的)
chrome 浏览器上可以如下图查看参数:
上图的 1 和 2 两种方式,restTemplate 采用如下方式发送请求
// path 参数
String url1 = "http:/192.168.0.11/a/xxx/" + pathParam+"/get.do";
restTemplate.delete(url1);
// 普通 requestParams
String idParam = "111";
String url2 = "http:/192.168.0.11/a/xxx/aa/get.do?id="+idParam ;
restTemplate.delete(url2);
第 2 类 requestBody 参数(post 和put最常见)
String url = "http:/192.168.0.11/a/xxx/123/recognize-text?groupId=aa&heightPriority=false";
String taskId = restTemplate.postForObject(url, new ParamBody(),
String.class);
//其中 ParamBody 如下
/**
* boday 请求参数
*
*
*/
public static class ParamBody {
private boolean enableRecognizePipelineNo = false;
public boolean isEnableRecognizePipelineNo() {
return enableRecognizePipelineNo;
}
public void setEnableRecognizePipelineNo(boolean enableRecognizePipelineNo) {
this.enableRecognizePipelineNo = enableRecognizePipelineNo;
}
}
如下图所示:红色框代表 body 参数,但上图将 post 中的 【query String Parameters】进行拼凑到了 url 后面。
第 3 种 File 类型(即上传文件)
情况1 :直接在 url 中 就是文件的地址(没测试过)
restTemplate.postForObject("http://localhost:8080/Lock/master/sendCmd", null, void.class);
情况 2:restTemplate从服务器文件系统中选文件上传
感谢博主 https://www.jianshu.com/p/a326a2ffafde
从文件系统上传文件时,需要把文件转换成FileSystemResource,然后放入HttpEntity中即可。
public UploadImageResponseVO uploadImg(String fileLocal) throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.setConnection("Keep-Alive");
headers.setCacheControl("no-cache");
FileSystemResource resource = new FileSystemResource(new File(fileLocal));
HttpEntity<FileSystemResource> httpEntity = new HttpEntity<>(resource);
ResponseEntity<Resource> responseEntity = restTemplate.postForEntity(serverUrl, httpEntity, Resource.class);
//对读到的内容映射成对应的实体类
XmlMapper xmlMapper = new XmlMapper();
UploadImageResponseVO uploadImageResponseVO = xmlMapper.readValue(responseEntity.getBody().getInputStream(), UploadImageResponseVO.class);
logger.debug("responseEntity: {} ", uploadImageResponseVO);
return uploadImageResponseVO;
}
情况3 :restTemplate转发接收的文件,直接上传图片。
用multipart形式上传文件时,需要用到MultiValueMap<String, Object>类,用它装载文件对象以及multipart的表单数据。
这里涉及到一个文件,从request中拿文件往MultiValueMap放时,不能直接request.getFile()给multiValueMap,这样在restTemplate上传文件时,messageConverter转化时会报错,需要把request.getFile("file").getResource()的数据给multiValueMap。代码如下:
public UploadResponseVO resendUpload(String url,StandardMultipartHttpServletRequest request) throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
parts.add("file",request.getFile("file").getResource());
Enumeration<String> enumeration= request.getParameterNames();
while(enumeration.hasMoreElements()){
String key = enumeration.nextElement();
parts.add(key,request.getParameter(key));
}
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(parts, headers);
ResponseEntity<UploadResponseVO> responseEntity = restTemplate.postForEntity(url,httpEntity,UploadResponseVO.class);
return responseEntity.getBody();
}
坑记录:
好奇的童鞋,对 restTemplate 的第四个参数(uriVariables)感兴趣,这个是对 第1类的path参数很有用。见如下代码:
String atlasId = "xx";
String index = "01";
String url = "http://192.168.1.11:8080/a/{id}/drawings/{name}/get.do";
//注意 {id}和真实参数的 atlasId 名称没关系,只和 postForObject 从第四个参数后的参数顺序有关
String taskId_createRecognizeSymbolTask = restTemplate.postForObject(url, null, String.class, atlasId, index);