RestTemplate常用方法以及个别示例

RestTemplate定义了36个与REST资源交互的方法,其中的大多数都对应于HTTP的方法。
共有11个独立的方法,其中十个有三种重载形式,而第十一个则重载了六次,所以形成了36个方法。

  • delete():在特定的URL上对资源执行HTTP DELETE操作;
  • exchange():在URL上执行特定的HTTP方法,返回包含对象的ResponseEntity,这个对象是从响应体中映射得到的;
  • execute():在URL上执行特定的HTTP方法,返回一个从响应体映射得到的对象;
  • getForEntity():发送一个HTTP GET请求,返回的ResponseEntity包含了响应体所映射成的对象;
  • getForObject():发送一个HTTP GET请求,返回的请求体将映射为一个对象;
  • postForEntity():POST 数据到一个URL,返回包含一个对象的ResponseEntity,这个对象是从响应体中映射得到的;
  • postForObject():POST 数据到一个URL,返回根据响应体匹配形成的对象;
  • headForHeaders():发送HTTP HEAD请求,返回包含特定资源URL的HTTP头;
  • optionsForAllow():发送HTTP OPTIONS请求,返回对特定URL的Allow头信息;
  • postForLocation():POST 数据到一个URL,返回新创建资源的URL;
  • put():PUT 资源到特定的URL;
  • getMessageConverters()

Get请求:

在这里插入图片描述
注:getForEntity()的返回值类型是ResponseEntity泛型类

看一下 ResponseEntity 的文档描述:
在这里插入图片描述
可以看到 它继承了HttpEntity. 封装了返回的响应信息,包括 响应状态,响应头 和 响应体.

测试getForEntity()方法:

  1. 无参数的 getForEntity 方法
@RequestMapping("getForEntity")
public Student getStu() {
    ResponseEntity<Student> responseEntity = restTemplate.getForEntity("http://localhost/getAll", Student.class);
    HttpHeaders headers = responseEntity.getHeaders();	//获取返回信息的请求头
    HttpStatus statusCode = responseEntity.getStatusCode();	
    int code = statusCode.value();	//获取返回信息的响应状态

    Student student= responseEntity.getBody();	//获取返回信息的响应体

    System.out.println(student.toString());
    return student;
}
  1. 有参数的 getForEntity 请求
@RequestMapping("getForEntity")
public UserEntity getById2(@RequestBody Student student) {
    ResponseEntity<UserEntity> responseEntity = restTemplate.getForEntity("http://localhost/get",student,UserEntity.class);
    UserEntity userEntity = responseEntity.getBody();
    return userEntity;
}
  1. 有参数的 get 请求,使用map封装参数
@RequestMapping("getForEntity")
public UserEntity getById2(@RequestBody Student student) {
    HashMap<String, Student> map = new HashMap<>();
    map.put("student",student);
        
    ResponseEntity<UserEntity> responseEntity = restTemplate.getForEntity("http://localhost/get",map,UserEntity.class);
    UserEntity userEntity = responseEntity.getBody();
    return userEntity;
}

注:通过以上代码,我们可以获取Http请求的全部信息(状态码,响应头,响应体)。但是,通常情况下我们并不想要Http请求的全部信息,只需要相应体即可.对于这种情况,RestTemplate提供了 getForObject() 方法用来只获取 响应体信息。getForObject 和 getForEntity 用法几乎相同,指示返回值返回的是 响应体,省去了我们再去 getBody() 。

测试getForObject()方法:

  1. 无参数的 getForObject方法
@RequestMapping("getForEntity")
public Student getStu() {
    Student student= restTemplate.getForObject("http://localhost/getAll", Student.class);
    System.out.println(student.toString());
    return student;
}
  1. 有参数的 getForObject请求
@RequestMapping("getForEntity")
public UserEntity getById2(@RequestBody Student student) {
    Student student= restTemplate.getForObject("http://localhost/get",student,Student.class);
    return student;
}
  1. 有参数的 get 请求,使用map封装参数
@RequestMapping("getForEntity")
public UserEntity getById2(@RequestBody Student student) {
    HashMap<String, Student> map = new HashMap<>();
    map.put("student",student);
        
    Student student = restTemplate.getForObject("http://localhost/get",map,Student.class);
    return student ;
}

Post请求:

测试postForEntity()方法:

  1. 无参数的 postForEntity方法
@RequestMapping("getForEntity")
public Student postStu() {
    ResponseEntity<Student> responseEntity = restTemplate.postForEntity("http://localhost/getAll", Student.class);
    HttpHeaders headers = responseEntity.getHeaders();	//获取返回信息的请求头
    HttpStatus statusCode = responseEntity.getStatusCode();	
    int code = statusCode.value();	//获取返回信息的响应状态

    Student student= responseEntity.getBody();

    System.out.println(student.toString());
    return student;
}

浏览器访问:
http://localhost/getForEntity?username=itguang&password=123456&age=20&email=123@123.com
返回的ResponseEntity包含:提交的数据,响应码,响应头,响应体

  1. 有参数的 postForEntity 请求
@RequestMapping("getForEntity")
public UserEntity getById2(@RequestBody Student student) {
    ResponseEntity<UserEntity> responseEntity = restTemplate.postForEntity("http://localhost/get",student,UserEntity.class);
    UserEntity userEntity = responseEntity.getBody();
    return userEntity;
}
  1. 以下示例不再一一列举,情况如此,并无很大差异;
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值