restTemplate 使用


参考https://www.jianshu.com/p/27a82c494413,侵权删(仅学习后敲一遍)

restTemplate

简述RestTemplate

是Spring用于同步client端的核心类,简化了与http服务的通信,并满足RestFul原则,程序代码可以给它提供URL,并提取结果。默认情况下,RestTemplate默认依赖jdk的HTTP连接工具。当然你也可以 通过setRequestFactory属性切换到不同的HTTP源,比如Apache HttpComponents、Netty和OkHttp。

RestTemplate能大幅简化了提交表单数据的难度,并且附带了自动转换JSON数据的功能,但只有理解了HttpEntity的组成结构(header与body),且理解了与uriVariables之间的差异,才能真正掌握其用法。这一点在Post请求更加突出,下面会介绍到。

GET 请求实践

getForObject()方法

getForObject()其实比getForEntity()多包含了将HTTP转成POJO的功能,但是getForObject没有处理response的能力。因为它拿到手的就是成型的pojo。省略了很多response的信息。

public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables){}
public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables)
public <T> T getForObject(URI url, Class<T> responseType)

带参数get请求

/**
 * get 请求 ,参数(占位符)传参
 */
@Test
public void testGet(){
    String api = "http://192.168.6.9:8060/ronghe/sendMessage/register?username={username}";
    User user = this.restTemplate.getForObject(api, User.class ,"13588888888");
    System.out.println(user);
}

/**
 * get 请求 ,用 map 传参
 */
@Test
public void testGet2(){
    String api = "http://192.168.6.9:8060/ronghe/sendMessage/register?username={username}";
    Map<String,String> map = new HashMap<>();
    map.put("username","13588888888");
    User user = this.restTemplate.getForObject(api, User.class ,map);
    System.out.println(user);
}

getForEntity()方法

public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables){}
public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables){}
public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType){}

带参数的get请求

/**
     * getForEntity()方法 , 有返回值
     */
    @Test
    public void testGEtForEntity(){
        String api = "http://192.168.6.9:8060/ronghe/exBox/countFreeBox?cabinetNo={cabinetNo}&boxClassify={boxClassify}";
        Map<String,String> map = new HashMap<>();
        map.put("cabinetNo","99999999");
        map.put("boxClassify","2");
        ResponseEntity<BoxTypeDTO> responseEntity = restTemplate.getForEntity(api,BoxTypeDTO.class,map );
        BoxTypeDTO boxTypeDTO = responseEntity.getBody();
    }

POST 请求实践

也分为 postForEntitygetForObject 方法

postForEntity 方法

postForObject 方法形参一致

 public <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables) throws RestClientException{}

    public <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {}

    public <T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException {}

返回的是ResponseEntity对象,如果需要转换成pojo,还需要json工具类的引入,这个按个人喜好用。不会解析json的可以百度FastJson或者Jackson等工具类。然后我们就研究一下ResponseEntity下面有啥方法。

ResponseEntity、HttpStatus、BodyBuilder结构

ResponseEntity.java

public HttpStatus getStatusCode(){}
public int getStatusCodeValue(){}
public boolean equals(@Nullable Object other) {}
public String toString() {}
public static BodyBuilder status(HttpStatus status) {}
public static BodyBuilder ok() {}
public static <T> ResponseEntity<T> ok(T body) {}
public static BodyBuilder created(URI location) {}
...

HttpStatus.java

public enum HttpStatus {
public boolean is1xxInformational() {}
public boolean is2xxSuccessful() {}
public boolean is3xxRedirection() {}
public boolean is4xxClientError() {}
public boolean is5xxServerError() {}
public boolean isError() {}
}

BodyBuilder.java

public interface BodyBuilder extends HeadersBuilder<BodyBuilder> {
    //设置正文的长度,以字节为单位,由Content-Length标头
      BodyBuilder contentLength(long contentLength);
    //设置body的MediaType 类型
      BodyBuilder contentType(MediaType contentType);
    //设置响应实体的主体并返回它。
      <T> ResponseEntity<T> body(@Nullable T body);

可以看出来,ResponseEntity包含了HttpStatus和BodyBuilder的这些信息,这更方便我们处理response原生的东西。

post 方法带参数

@Test
public void test4(){
     String api = "http://192.168.6.9:8060/ronghe/exBox/countFreeBox";
     BoxCountDTO boxCountDTO = new BoxCountDTO();
     boxCountDTO.setBoxClassify("2");
     boxCountDTO.setCabinetNo("99999999");
     ResponseEntity<BoxTypeDTO> responseEntity = restTemplate.postForEntity(api , boxCountDTO, BoxTypeDTO.class);
     BoxTypeDTO body = responseEntity.getBody();
 }

get 方法带参数带请求头
我习惯用String.class 去接收返回值,然后通过Json 转换,获取对象。
个人不建议和别人对接接口的时候,直接用对象的形式去返回(因为之前项目的接口被人改了,也不知道自己去改)

@Test
    public void test4(){
        String api = "http://192.168.6.9:8060/ronghe/exBox/countFreeBox";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); // 注意,自己测试的时候,需要把@RequestBody 注解去掉
        MultiValueMap<String, String> map= new LinkedMultiValueMap<>(); 
        map.add("cabinetNo", "99999999");
        map.add("boxClassify","3");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity( api, request , String.class );
        String body = response.getBody();

也可以全局设置请求头,但是好似一个完美的解决方案,实质太局限性的操作了,我还是喜欢每个请求里写上,防止耦合…

未完待续。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RestTemplate是Spring提供的一个用于访问RESTful Web服务的客户端工具类,它封装了HTTP请求和响应的细节,使得开发者可以方便地进行RESTful Web服务的调用。 使用RestTemplate需要进行以下步骤: 1. 创建RestTemplate实例 可以使用以下代码创建一个RestTemplate实例: ``` RestTemplate restTemplate = new RestTemplate(); ``` 2. 发送HTTP请求 可以使用RestTemplate的一些方法发送HTTP请求,例如: ``` // GET请求 String result = restTemplate.getForObject(url, String.class); // POST请求 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<String> entity = new HttpEntity<>(requestBody, headers); String result = restTemplate.postForObject(url, entity, String.class); ``` 其中,`url`表示请求的URL,`String.class`表示请求返回的数据类型,`requestBody`表示POST请求中的请求体。 3. 处理HTTP响应 RestTemplate提供了一些方法用于处理HTTP响应,例如: ``` // GET请求 ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); HttpStatus statusCode = response.getStatusCode(); String responseBody = response.getBody(); // POST请求 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<String> entity = new HttpEntity<>(requestBody, headers); ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class); HttpStatus statusCode = response.getStatusCode(); String responseBody = response.getBody(); ``` 其中,`ResponseEntity`表示HTTP响应的实体类,包含了响应头、响应状态码和响应体等信息,`statusCode`表示响应状态码,`responseBody`表示响应体。 以上就是使用RestTemplate的基本流程。在实际的开发中,可以根据实际需求选择不同的RestTemplate方法来发送HTTP请求和处理HTTP响应。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值