相关文章推荐
天涯  ·  Spring Cloud ...·  1 年前    · 
天涯  ·  SpringCloud —— ...·  2 年前    · 
天涯  ·  Spring ...·  3 年前    · 
天涯  ·  java - ...·  3 年前    · 
zhiwen.tan  ·  Hystrix命令入门使用 - - ...·  4 年前    · 
  1. RestTemplate接口
    1.1 GET接口
     public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables)
     public <T> T getForObject(URI url, Class<T> responseType)
     public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables)
1.2 POST接口
     public <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables) 
     public <T> T postForObject(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
     public <T> T postForObject(URI url, Object request, Class<T> responseType)
1.3 带header的http请求接口
     public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> 
            responseType, Object... uriVariables)
     public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> 
            responseType, Map<String, ?> uriVariables)
     public <T> ResponseEntity<T> exchange(URI url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> 
            responseType)       
1.4 ......

2. 使用方法
2.1 GET请求

    RestTemplate restTemplate=new RestTemplate();
    String res=restTemplate.getForObject("http://test.com",String.class);
    String param1="1",param2="2";
    String res=restTemplate.getForObject("http://test.com?param1={param1}&param2=          
           {param2}",String.class,param1,param2);
    可将param1和param2组合成map
2.2POST请求
        RestTemplate restTemplate=new RestTemplate();
        String res=restTemplate.postForObject("http://test.com",null,String.class);
        MultiValueMap<String,String> multiValueMap=new LinkedMultiValueMap<String, String>();
        multiValueMap.add("param1","1");
        multiValueMap.add("param2","2");
        String res=restTemplate.postForObject("http://test.com",multiValueMap,String.class);
2.3Exchange请求
        RestTemplate restTemplate=new RestTemplate();
        String res=restTemplate.exchange("http://test.com",HttpMethod.GET,null,String.class);
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.add("param1","1");
        httpHeaders.add("param2","2");
        restTemplate.exchange("http://test.com", HttpMethod.GET,new HttpEntity<Object>
            (httpHeaders),String.class);
RestTemplate接口 1.1 GET接口 public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables) public <T> T getForObject(URI url, Class<T> responseType) publ 如下代码,url=http://www.baidu.com时请求报400,但是url使用http://www.baidu.com?type=1时请求正常,只要带?了的参数均可以。 HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.add("Authorization", authorizati...
RestTemplate是什么 RestTemple是Spring提供的用于访问Http请求的客户端,RestTemple提供了多种简洁的远程访问服务的方法,省去了很多无用的代码。 为什么要用RestTemplate 相信大家之前都用过apache的HTTPClient类,逻辑繁琐,代码复杂,还要自己编写使用类HttpClientUtil,封装对应的post,get,delete等方法。 Rest...
参考:https://blog.csdn.net/jinjiniao1/article/details/100849237 博主:江南一点雨 本文归纳自 慕课网 江南一点雨老师 的学习笔记,大家可以购买他的课程学习。 GET请求 API:getForEntity 与 getForObject 相同:可传递参数 区别:返回值类型 getForEntity:返回ResponseEntity实例,内部包含响应数据与响应头 getForObject:仅返回指定类型的响应数据 getForEntity的使
RestTemplatespring封装的restful请求的模板,它内部封装了常用的GET、POST、DELETE、PUT等请求方式,帮助开发者更快构建HTTP请求。传统的请求方式采用Apache的HttpClient,此种方式编写http请求时需要编写大量代码,比较繁琐。本文将从代码量、以及最大访问量对比HttpClient与Restemplate请求封装的工具类。 2.基于HttpClient封装的工具类 2.1 GET请求 2.2 POST请求 2.3 DELETE请求 2.4 PUT请
restTemplate.getForEntity慢极大可能是序列化的方面的问题。 采用FastJSON直接将restTemplate.getForEntity转换为JsonObject进行接口调用的返回,速度显著提示 “====================”紧接着在“uri: http://localhost:8080/platform/api/order/full/17001”的后面被打印出来,说明restTemplate.getForEntity是不慢的 用FastJSON中的JSON.to
Spring RestTemplateSpring框架中的一个重要组件,用于简化HTTP请求的发送。使用RestTemplate可以实现对RESTful Web服务的访问,支持GET、POST、PUT、DELETE等常见的HTTP请求方法。 下面是Spring RestTemplate的一些基本使用方法: 1. 创建RestTemplate实例: ```java RestTemplate restTemplate = new RestTemplate(); 2. 发送GET请求: ```java String url = "http://example.com/users/{userId}"; ResponseEntity<User> response = restTemplate.getForEntity(url, User.class, userId); 3. 发送POST请求: ```java String url = "http://example.com/users"; User user = new User(); ResponseEntity<User> response = restTemplate.postForEntity(url, user, User.class); 4. 发送PUT请求: ```java String url = "http://example.com/users/{userId}"; User user = new User(); restTemplate.put(url, user, userId); 5. 发送DELETE请求: ```java String url = "http://example.com/users/{userId}"; restTemplate.delete(url, userId); RestTemplate还有很多其他的方法,更详细的使用方法可以参考Spring官方文档。