在某种状况下,后台服务可能须要访问另外一台服务器的REST接口。之前估计很多人用的都是HttpRequest类来着,结合Paser解析JSON格式的Body。如今Spring Boot的Web Starter就自带了RestTemplate,直接用它的就行了。最好不要再往项目里导新的依赖。这里作了点整理,分享出来。发数据库
1、GET请求数组
案例以下:服务器
RestTemplate restTemplate = new RestTemplate(); ResponseEntity<CustomClass> responseEntity = restTemplate.getForEntity(url, CustomClass.class);
CustomClass response = responseEntity.getBody();
(1)getForEntity 顾名思义就是发送GET请求,并获得一个对象app
(2)这里的URL应该是完整的,即应该是这样的格式:"http://域名:端口/xxx/yyy"ide
(3)CustomClass是你本身定义的类,tempalte会将请求响应的JSON格式的body解析成CustomClass。函数
(4)ResponseEntity是请求响应的结果,能够获取响应码,以及最重要的响应Bodypost
注意:因为RestTemplate是使用Jackson来进行映射的,而Jackson本质上是用setter/getter来实现的。其中JSON字符串转对象须要setter,对象转JSON字符串须要getter。这里是字符转对象,因此你定义的类必须有setter。this
2、POST请求url
案例以下:spa
... restTemplate.postForEntity(url, ParamObject, Response.class); ...
基本操做同上,不过postForEntity的第二个参数是请求对象,底层会将其转为JSON字符存入请求Body中
1、普通的表单请求
//用MultiValueMap存储表单数据 MultiValueMap<String, Object> param = new LinkedMultiValueMap<>(); param.add("param1","1232"); param.add("param2","12312"); //用HttpEntity进行包装 HttpEntity<MultiValueMap<String,Object>> httpEntity = new HttpEntity<>(param); //发送请求 ResponseEntity<Response> responseEntity = restTemplate.postForEntity(url, httpEntity, Response.class);
这就相对于提交了一个普通的表单
2、带文件的表单请求
有时候咱们须要把本地文件经过Post请求发送给另外一个应用,能够这么作。而对方接收的时候就用@RequestParam("file") MultipartFile来接收。
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>(); //FileSystemResource是一个包装类,好统一处理 param.add("file", new FileSystemResource(new File(filePath))); ...
(1)网页上经过表单上传文件,默认标识符就是"file"
(2)通过FileSystemResource包装后,RestTemplate会利用File对象的InputStream从磁盘中读入文件内容,并写入HTTP请求body中。
3、带文件的表单请求——文件从客户端而来(MultipartFile)
//file由客户端上传过来,直接转发,不通过磁盘 @PostMapping("/some-url") public SomeResponse function(@RequestParam("file") MultipartFile file, ...) { ... MultiValueMap<String, Object> param = new LinkedMultiValueMap(); param.add("file", new MultipartFileResource(file)); ... }
MultipartFileResource相似FileSystemResource
4、带文件的表单请求——本地没有文件,只有文件的文本内容
可能存在这种状况,即你本地保存的是文本文件的内容,(如保存在数据库中,用text存储。由于这样比较方便管理)而另外一个服务器须要以文件的形式接收你的文本内容。即它会用一个MultipartFile来接收。这时候首先你从数据库中把文本内容读出来,传统数据库存储位置就在磁盘,这已是不小的开销了,难道你还要把文本以文件的形式写到磁盘再传输吗?这种操做是很是愚蠢的。既然咱们已经知道请求本质上传递的是字节数组,那何不就仿造FileSystemResource写一个适配类呢?
//该类与FileSystemResouce同父类 public class TextFeignFileResource extends AbstractResource { //文本的字节信息 private byte[] bytes; //当前读到的位置 private int index = 0; //文件名 private String filename; //构造函数须要两个参数,一个是文件名,随便取 //另外一个就是文本内容,String类型足够存文本的 public TextFeignECLFileResource(String filename, String content) { this.filename = filename; this.bytes = content.getBytes(); } @Override public String getDescription() { return null; } @Override public InputStream getInputStream() throws IOException { return new InputStream() { //注意这里的返回值int是一个八位的字节的数值表示 //并非读取到的字节个数 @Override public int read() throws IOException { if (index >= bytes.length) return -1; //返回-1是标准,表示读取结束 return bytes[index++]; } }; } @Override public boolean exists() { return true; } @Override public boolean isFile() { return true; } @Override public boolean isReadable() { return true; } @Override public long contentLength() throws IOException { return bytes.length; } @Override public String getFilename() { return filename; } }
使用方式:
MultiValueMap<String, Object> param = new LinkedMultiValueMap<>(); param.add("file", new TextFeignFileResource("test.txt", "hello world"));
注意:这里之因此能这么作是由于普通的文本文件没有文件头等额外的描述信息
使用Jodd的Http API会更方便一点。