当前位置:   article > 正文

java之restTemplate请求接口get、post封装及自定义请求头封装_resttemplate get请求参数封装

resttemplate get请求参数封装

本套接口封装了

1.通过restTemplate发起的Get、POST请求

2.通过restTemplate发起的Get、POST请求,可自定义请求头

使用本套api发起get/post请求只需要简单传参 url、请求头、请求体

  1. package x.bfb6.cn.bfb_km.utils;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.springframework.http.*;
  4. import org.springframework.web.client.RequestCallback;
  5. import org.springframework.web.client.ResponseExtractor;
  6. import org.springframework.web.client.RestTemplate;
  7. import java.util.Iterator;
  8. import java.util.Map;
  9. import java.util.Objects;
  10. public class HttpGetAndPost {
  11. /**
  12. * 通用post请求封装
  13. * @param url
  14. * @param jsonData
  15. * @return
  16. */
  17. public String httpPost(String url, JSONObject jsonData){
  18. RestTemplate restTemplate = new RestTemplate();
  19. //设置Http Header
  20. HttpHeaders headers = new HttpHeaders();
  21. headers.add("Content-Type","application/json");
  22. headers.add("Accept","application/json, text/plain, */*");
  23. //设置请求媒体数据类型
  24. headers.setContentType(MediaType.APPLICATION_JSON);
  25. //设置返回媒体数据类型
  26. headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  27. HttpEntity<String> formEntity = new HttpEntity<String>(jsonData.toString(), headers);
  28. return restTemplate.postForObject(url, formEntity, String.class);
  29. }
  30. /**
  31. * 通用get请求
  32. * @param url
  33. * @return
  34. */
  35. public String httpGet(String url){
  36. RestTemplate restTemplate = new RestTemplate();
  37. //设置Http Header
  38. HttpHeaders headers = new HttpHeaders();
  39. headers.add("Content-Type","application/json");
  40. headers.add("Accept","application/json, text/plain, */*");
  41. //设置请求媒体数据类型
  42. headers.setContentType(MediaType.APPLICATION_JSON);
  43. //设置返回媒体数据类型
  44. headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  45. return restTemplate.getForObject(url, String.class);
  46. }
  47. /**
  48. * 自定义请求头的post请求
  49. * @param url
  50. * @param jsonData
  51. * @param jsonHeaders
  52. * @return
  53. */
  54. public String httpPost(String url, JSONObject jsonData,JSONObject jsonHeaders){
  55. RestTemplate restTemplate = new RestTemplate();
  56. //设置Http Header
  57. HttpHeaders headers = getHeaders(jsonHeaders);
  58. //设置请求媒体数据类型
  59. headers.setContentType(MediaType.APPLICATION_JSON);
  60. //设置返回媒体数据类型
  61. headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  62. HttpEntity<String> formEntity = new HttpEntity<String>(jsonData.toString(), headers);
  63. return restTemplate.postForObject(url, formEntity, String.class);
  64. }
  65. /**
  66. * 自定义请求头的get请求
  67. * @param url
  68. * @param jsonHeaders
  69. * @return
  70. */
  71. public String httpGet(String url,JSONObject jsonHeaders){
  72. RestTemplate restTemplate = new RestTemplate();
  73. HttpHeaders headers = getHeaders(jsonHeaders);
  74. HttpEntity request = new HttpEntity(headers);
  75. // 构造execute()执行所需要的参数。
  76. RequestCallback requestCallback = restTemplate.httpEntityCallback(request, JSONObject.class);
  77. ResponseExtractor<ResponseEntity<JSONObject>> responseExtractor = restTemplate.responseEntityExtractor(JSONObject.class);
  78. // 执行execute(),发送请求
  79. return Objects.requireNonNull(Objects.requireNonNull(restTemplate.execute(url, HttpMethod.GET, requestCallback, responseExtractor)).getBody()).toString();
  80. }
  81. /**
  82. * json转化请求头
  83. * @param jsonHeaders
  84. * @return
  85. */
  86. public static HttpHeaders getHeaders(JSONObject jsonHeaders){
  87. Iterator iterator = jsonHeaders.entrySet().iterator();
  88. HttpHeaders headers = new HttpHeaders();
  89. while (iterator.hasNext()){
  90. Map.Entry entry = (Map.Entry) iterator.next();
  91. headers.add(entry.getKey().toString(),entry.getValue().toString());
  92. }
  93. return headers;
  94. }
  95. }

测试类

  1. package x.bfb6.cn.bfb_km;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import org.springframework.http.HttpEntity;
  6. import org.springframework.http.HttpHeaders;
  7. import org.springframework.http.MediaType;
  8. import org.springframework.web.client.RestTemplate;
  9. import x.bfb6.cn.bfb_km.utils.HttpGetAndPost;
  10. import java.util.Iterator;
  11. import java.util.Map;
  12. @SpringBootTest
  13. public class HttpTest {
  14. HttpGetAndPost httpGetAndPost = new HttpGetAndPost();
  15. //测试get请求
  16. @Test
  17. public void getTest(){
  18. String data = httpGetAndPost.httpGet("http://localhost:8081/api/v1/pub/video/list");
  19. System.out.println("get请求成功");
  20. System.err.println(data);
  21. }
  22. //测试post请求
  23. @Test
  24. public void postTest(){
  25. String pwd ="6515551",phone = "651561";
  26. JSONObject jsonObject = new JSONObject();
  27. JSONObject jsonData = jsonObject.fluentPut("phone", phone).fluentPut("pwd", pwd);
  28. String data = httpGetAndPost.httpPost("http://localhost:8081/api/v1/pri/user/login",jsonData);
  29. System.out.println("post请求成功");
  30. System.err.println(data);
  31. }
  32. //测试get请求自定义请求头
  33. @Test
  34. public void getCustomHeaderTest(){
  35. String token ="xdclasseyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4ZGNsYXNzIiwiaGVhZF9pbWciOiJodHRwczovL3hkLXZpZGVvLXBjLWltZy5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20veGRjbGFzc19wcm8vZGVmYXVsdC9oZWFkX2ltZy8xNC5qcGVnIiwiaWQiOjMxLCJuYW1lIjoiNTY1NTYiLCJpYXQiOjE2NTM0NjI0NDYsImV4cCI6MTY1NDA2NzI0Nn0.VGXePtXixZEq28l-6AXk8C1dFsVBojHbKb_fJ1ArXrk";
  36. JSONObject jsonObject = new JSONObject();
  37. JSONObject jsonData = jsonObject.fluentPut("token", token);
  38. String data = httpGetAndPost.httpGet("http://localhost:8081/api/v1/pri/user/find_by_token",jsonData);
  39. System.out.println("get带请求头请求成功");
  40. System.err.println(data);
  41. }
  42. //测试get请求自定义请求头
  43. @Test
  44. public void postCustomHeaderTest(){
  45. String video_id ="40",token = "xdclasseyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4ZGNsYXNzIiwiaGVhZF9pbWciOiJodHRwczovL3hkLXZpZGVvLXBjLWltZy5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20veGRjbGFzc19wcm8vZGVmYXVsdC9oZWFkX2ltZy8xNC5qcGVnIiwiaWQiOjMxLCJuYW1lIjoiNTY1NTYiLCJpYXQiOjE2NTM0NjI0NDYsImV4cCI6MTY1NDA2NzI0Nn0.VGXePtXixZEq28l-6AXk8C1dFsVBojHbKb_fJ1ArXrk";
  46. //设置请求体
  47. JSONObject jsonObject = new JSONObject();
  48. JSONObject jsonData = jsonObject.fluentPut("video_id", video_id);
  49. //设置请求头
  50. JSONObject jsonObject2 = new JSONObject();
  51. JSONObject jsonData2 = jsonObject2.fluentPut("token", token);
  52. String data = httpGetAndPost.httpPost("http://localhost:8081/api/v1/pri/order/save",jsonData,jsonData2);
  53. System.out.println("post带请求头请求成功");
  54. System.err.println(data);
  55. }
  56. @Test
  57. public void test(){
  58. getTest();
  59. postTest();
  60. getCustomHeaderTest();
  61. postCustomHeaderTest();
  62. }
  63. }

测试结果

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/303041
推荐阅读
相关标签
  

闽ICP备14008679号