RestTemplate详解
程序员文章站
2022-04-28 17:29:27
...
一、GET请求
在RestTemplate中,对GET请求可以通过两个方法进行调用实现
第一种:getForEntity(String url,Class responseType,Object ....urlvaribales);该方法提供三个参数
- url为请求的地址
- responseType为请求响应体body的包装类型
- urlvaribales,配合urlvaribales 参数实现GET请求的参数绑定,替换url中的占位符
//getForEntity(String url,Class responseType,Object ....urlvaribales)
String url = "http://localhost:8088/test?str={1}";
ResponseEntity<Integer> re = restTemplate.getForEntity(url,Integer.class,"123456");
int result = re.getBody();
//还可以使用Map传参
//getForEntity(String url,Class responseType,Map urlvaribales)
String url = "http://localhost:8088/test?str={str}";
Map<String,String> params = new HashMap<>();
params.put("str","123456");
ResponseEntity<Integer> re = restTemplate.getForEntity(url,Integer.class,params);
int result = re.getBody();
第二种 :getForObject 函数,该方法可以理解为对getForEntityde 进一步封装转换,实现请求直接返回包装好的对象内容,比如。
//1.getForObject(String url,Class responseType,Object ....urlvaribales)
String url = "http://localhost:8088/test?str={1}";
int result = restTemplate.getForObject(url,Integer.class,"123456");
//2.getForObject(String url,Class responseType,Map urlvaribales)
String url = "http://localhost:8088/test?str={str}";
Map<String,String> params = new HashMap<>();
params.put("str","123456");
int result = restTemplate.getForObject(url,Integer.class,params);
二、POST请求
第一种:postForEntity函数。该方法同GET请求中的getForEntity相似。其中有三种不同的重载方法如下所示
-
postForEntity(String url,Object request, Class<T> responseType, Object... uriVariables)
-
postForEntity(String url,Object request, Class<T> responseType, Map uriVariables)
-
postForEntity(String url,Object request, Class<T> responseType)
其中每一个参数解释如下:
- url为请求的地址
- request对象参数
- responseType为请求响应体body的包装类型
- urlvaribales,配合urlvaribales 参数实现GET请求的参数绑定,替换url中的占位符
实际操作如下代码
String url = "http://localhost:8088/getbean?str={1}";
TOrderCommunicationRecord tOrderCommunicationRecord= new TOrderCommunicationRecord();
tOrderCommunicationRecord.setOrdernumber("12345678912345678912345678912343");
tOrderCommunicationRecord.setCommunicationtime("2019-10-23 12:00:00");
tOrderCommunicationRecord.setOperationperson("232");
tOrderCommunicationRecord.setRecordcontent("45");
tOrderCommunicationRecord.setUsername("565");
ResponseEntity<TOrderCommunicationRecord> re = restTemplate.postForEntity(url,tOrderCommunicationRecord,TOrderCommunicationRecord.class,"123456");
TOrderCommunicationRecord result = re.getBody();
//使用Map调用时请参考GET请求代码中的Map传参的使用
第二种:postForObject函数。该函数也实现了三种不同的重载方法,分别为:
- postForObject(String url, Object request, Class<T> responseType, Object... uriVariables)
- postForObject(String url, Object request, Class<T> responseType, Map uriVariables)
- postForObject(String url, Object request, Class<T> responseType)
对所列第一个方法代码案列如下:
String url = "http://localhost:8088/getbean?str={1}";
TOrderCommunicationRecord tOrderCommunicationRecord= new TOrderCommunicationRecord();
tOrderCommunicationRecord.setOrdernumber("12345678912345678912345678912343");
tOrderCommunicationRecord.setCommunicationtime("2019-10-23 12:00:00");
tOrderCommunicationRecord.setOperationperson("232");
tOrderCommunicationRecord.setRecordcontent("45");
tOrderCommunicationRecord.setUsername("565");
TOrderCommunicationRecord result = restTemplate.postForObject(url,tOrderCommunicationRecord,TOrderCommunicationRecord.class,"123456");
第三种:postForLocation函数,该函数实现了以POST请求提交的资源,并返回新资源的URI,该URI就相当于指定了返回类型,所以方法中就不需要再指定返回类型了。该函数也有三种不同的重载方法
- postForLocation(String url ,Object request, Object... uriVariables)
- postForLocation(String url ,Object request, Map uriVariables)
- postForLocation(String url ,Object request)
三、PUT请求
在RestTemplate中对PUT请求直接调用put方法进行调用实现,put函数也实现了三种不同的重载方法。put函数为void类型。
- put(String url ,Object request, Object... uriVariables)
- put(String url ,Object request, Map uriVariables)
- put(String url ,Object request)
四、DELETE请求
在RestTemplate中对DELETE请求直接调用DELETE方法进行调用实现,DELETE函数也实现了三种不同的重载方法。DELETE函数为void类型。
- delete(String url ,Object request, Object... uriVariables)
- delete(String url ,Object request, Map uriVariables)
- delete(String url ,Object request)