RestTemplate详解

目录

一、GET请求

二、POST请求

三、PUT请求

四、DELETE请求

五、处理请求和响应 headers


一、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)

五、处理请求和响应 headers

     除了上面描述的方法之外,RestTemplate还有exchange()方法,可以用于基于HttpEntity class 的任意 HTTP 方法执行。也许最重要的是,exchange()方法可用于添加请求 headers 和读取响应 headers。例如:

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");
HttpHeaders headers = new HttpHeaders();
headers.set("AUTO","asdasdasdasd");
HttpEntity httpEntity = new HttpEntity(tOrderCommunicationRecord, headers);
HttpEntity<TOrderCommunicationRecord> result = restTemplate.exchange(url, HttpMethod.POST,httpEntity,TOrderCommunicationRecord.class,"213123");
System.out.println(result.getBody().toString());

   

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值