RestTemplate使用详解

RestTemplate使用详解,第1张

RestTemplate使用详解 一、什么是RestTemplate

        RestTemplate 是从 Spring3.0 开始支持的一个 HTTP 请求工具,它提供了常见的REST请求方案的模版,例如 GET 请求、POST 请求、PUT 请求、DELETE 请求以及一些通用的请求执行方法 exchange 以及 execute。RestTemplate 继承自 InterceptingHttpAccessor 并且实现了 RestOperations 接口,其中 RestOperations 接口定义了基本的 RESTful *** 作,这些 *** 作在 RestTemplate 中都得到了实现。

        传统情况下在java代码里访问Restful服务,一般使用Apache的HttpClient。不过此种方法使用起来太繁琐。Spring提供了一种简单便捷的模板类RestTemplate来进行 *** 作:

定义一个简单的例子:

@RestController
public class TestController
{
    @RequestMapping(value = "testPost", method = RequestMethod.POST)
    public ResponseBean testPost(@RequestBody RequestBean requestBean)
    {
        ResponseBean responseBean = new ResponseBean();
        responseBean.setRetCode("0000");
        responseBean.setRetMsg("succ");

        return responseBean;
    }
}

使用RestTemplate访问该服务:

        //请求地址
        String url = "http://localhost:8080/testPost";
        //入参
        RequestBean requestBean = new RequestBean();
        requestBean.setTest1("1");
        requestBean.setTest2("2");
        requestBean.setTest3("3");

        RestTemplate restTemplate = new RestTemplate();
        ResponseBean responseBean = restTemplate.postForObject(url, requestBean, ResponseBean.class);

使用restTemplate访问restful接口非常的简单粗暴无脑。(url, requestBean, ResponseBean.class)这三个参数分别代表 请求地址、请求参数、HTTP响应转换被转换成的对象类型。

二、RestTemplate 添加请求头headers和请求体body
//headers & cookie
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
 
headers.add("basecret", config.getbasecret());
headers.add("baid", config.getBaid());
 
List cookies = new ArrayList<>();
cookies.add("cookie_USER" + Strings.nullToEmpty(config.getcookie()));
headers.put(HttpHeaders.cookie, cookies);
(1)POST请求

1、 调用postForObject方法   2、使用postForEntity方法  3、调用exchange方法
    postForObject和postForEntity方法的区别主要在于可以在postForEntity方法中设置header的属性,当需要指定header的属性值的时候,使用postForEntity方法。exchange方法和postForEntity类似,但是更灵活,exchange还可以调用get请求。使用这三种方法传递参数,Map不能定义为以下两种类型

Map paramMap = new HashMap();
Map paramMap = new linkedHashMap();

把Map类型换成linkedMultiValueMap后,参数成功传递到后台

MultiValueMap paramMap = new linkedMultiValueMap();

MultiValueMap paramMap = new linkedMultiValueMap();
paramMap.add("dt", "20190225");
 
// 1、使用postForObject请求接口
String result = template.postForObject(url, paramMap, String.class);
 
// 2、使用postForEntity请求接口
HttpHeaders headers = new HttpHeaders();
HttpEntity> httpEntity = new HttpEntity>(paramMap,headers);
ResponseEntity response2 = template.postForEntity(url, httpEntity, String.class);
 
// 3、使用exchange请求接口
ResponseEntity response3 = template.exchange(url, HttpMethod.POST, httpEntity, String.class);
 

如果post请求体是个Json的表单

        //JSonObject userInfo = new JSonObject();
        Map userInfo = Maps.newHashMap();
        userInfo.put("phone", accountForm.getPhone());
        userInfo.put("job", accountForm.getJob());
        userInfo.put("email", accountForm.getEmail());

        Map postBody = Maps.newHashMap();
        postBody.put("userInfo", userInfo);
 
        HttpEntity requestEntity = new HttpEntity<>(postBody, headers);
 
         try {
             ResponseEntity result = restTemplate.postForEntity(config.getCreateWithAuthUrl(), requestEntity, String.class);
             JsonNode jsonNode = JsonUtils.toJsonNode(result.getBody());
             if (jsonNode.get("errno").asInt() == 200 || jsonNode.get("errno").asInt() == 505) {
                 return true;
             }
 
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

(2)GET请求

如果是get请求,又想要把参数封装到map里面进行传递的话,Map需要使用HashMap,且url需要使用占位符,如下

RestTemplate restTemplate2 = new RestTemplate();
String url = "http://127.0.0.1:8081/interact/getData?dt={dt}&ht={ht}";
       
// 封装参数,这里是HashMap
Map paramMap = new HashMap();
paramMap.put("dt", "20190225");
paramMap.put("ht", "10");
 
//1、使用getForObject请求接口
String result1 = template.getForObject(url, String.class, paramMap);
System.out.println("result1====================" + result1);
 
//2、使用exchange请求接口
HttpHeaders headers = new HttpHeaders();
headers.set("id", "lidy");
HttpEntity> httpEntity = new HttpEntity>(null,headers);
ResponseEntity response2 = template.exchange(url, HttpMethod.GET, httpEntity, String.class,paramMap);
三、请求示例:


1、GET

private static void getEmployees(){
    final String uri = "http://localhost:8080/springrestexample/employees";
     
    RestTemplate restTemplate = new RestTemplate();
    String result = restTemplate.getForObject(uri, String.class);
     
    System.out.println(result);
}

2、使用RestTemplate定制HTTP头文件

private static void getEmployees(){
    final String uri = "http://localhost:8080/springrestexample/employees";
     
    RestTemplate restTemplate = new RestTemplate();
     
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    HttpEntity entity = new HttpEntity("parameters", headers);
     
    ResponseEntity result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
     
    System.out.println(result);
}

3、Get请求获取响应为一个对象

private static void getEmployees(){
    final String uri = "http://localhost:8080/springrestexample/employees";
    RestTemplate restTemplate = new RestTemplate();
     
    EmployeeListVO result = restTemplate.getForObject(uri, EmployeeListVO.class);
     
    System.out.println(result);
}

4、URL 参数

private static void getEmployeeById(){
    final String uri = "http://localhost:8080/springrestexample/employees/{id}";
     
    Map params = new HashMap();
    params.put("id", "1");
     
    RestTemplate restTemplate = new RestTemplate();
    EmployeeVO result = restTemplate.getForObject(uri, EmployeeVO.class, params);
     
    System.out.println(result);
}

5、POST

private static void createEmployee(){
    final String uri = "http://localhost:8080/springrestexample/employees";
 
    EmployeeVO newEmployee = new EmployeeVO(-1, "Adam", "Gilly", "test@email.com");
 
    RestTemplate restTemplate = new RestTemplate();
    EmployeeVO result = restTemplate.postForObject( uri, newEmployee, EmployeeVO.class);
 
    System.out.println(result);
}

6、Submit Form Data

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
 
MultiValueMap map= new linkedMultiValueMap<>();
map.add("id", "1");
 
HttpEntity> request = new HttpEntity<>(map, headers);
 
RestTemplate restTemplate = new RestTemplate();
EmployeeVO result = restTemplate.postForObject( uri, request, EmployeeVO.class);
System.out.println(result);

7、PUT

private static void updateEmployee(){
    final String uri = "http://localhost:8080/springrestexample/employees/{id}";
     
    Map params = new HashMap();
    params.put("id", "2");
     
    EmployeeVO updatedEmployee = new EmployeeVO(2, "New Name", "Gilly", "test@email.com");
     
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.put ( uri, updatedEmployee, params);
}

8、Simple PUT 

Foo updatedInstance = new Foo("newName");
updatedInstance.setId(createResponse.getBody().getId());
String resourceUrl = 
  fooResourceUrl + '/' + createResponse.getBody().getId();
HttpEntity requestUpdate = new HttpEntity<>(updatedInstance, headers);
template.exchange(resourceUrl, HttpMethod.PUT, requestUpdate, Void.class);
使用.exchange和请求回调
RequestCallback requestCallback(final Foo updatedInstance) {
    return clientHttpRequest -> {
        ObjectMapper mapper = new ObjectMapper();
        mapper.writevalue(clientHttpRequest.getBody(), updatedInstance);
        clientHttpRequest.getHeaders().add(
          HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
        clientHttpRequest.getHeaders().add(
          HttpHeaders.AUTHORIZATION, "Basic " + getbase64EncodedLogPass());
    };
}

9、DELETE

private static void deleteEmployee(){
    final String uri = "http://localhost:8080/springrestexample/employees/{id}";
     
    Map params = new HashMap();
    params.put("id", "2");
     
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.delete ( uri,  params );
}

10、发送带header的Post请求

// header填充
linkedMultiValueMap headers = new linkedMultiValueMap<>();
headers.put("Content-Type", Collections.singletonList("application/json;charset=UTF-8"));
headers.put("signature", Collections.singletonList(makeSignature(form.getNewMobile())));
// body填充
JSonObject json = new JSonObject();
json.put("oldMobile", mobile);
json.put("newMobile", form.getNewMobile());
HttpEntity request = new HttpEntity(json.toString(), headers);
// 一个单例的restTemplate
RestTemplate restTemplate = HttpInvoker.getRestTemplate();
// 发送请求
ResponseEntity response = restTemplate.postForEntity(whiteListURL, request, String.class);
        String url = prometheusServer + api;
        MultiValueMap map = new linkedMultiValueMap<>();
        // 查询参数
        map.add("query", sql.toString());
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        HttpEntity> request = new HttpEntity<>(map, headers);
        // 接受响应的类要与 接口响应一致,不然报错
        ResponseEntity exchange = restTemplate.postForEntity(url, request, PostResultVo.class);
        if (null != exchange && null != exchange.getBody() && null != exchange.getBody().getData()) {
           
        }

11、发送带header的Get请求

     
    @Test
    public void getHasHeader() {
        long userId = 32L;
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", "123");
        ResponseEntity response = restTemplate.exchange(
                "http://127.0.0.1:8280/user/{id}",
                HttpMethod.GET,
                new HttpEntity(headers),
                UserBean.class,
                userId);
        logger.info("response={}", JSON.toJSonString(response.getBody()));
    }
private static void getEmployees(){
    final String uri = "http://localhost:8080/springrestexample/employees";
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    HttpEntity entity = new HttpEntity("parameters", headers); 
    ResponseEntity result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
    System.out.println(result);
}

 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5136943.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存