赞
踩
本套接口封装了
1.通过restTemplate发起的Get、POST请求
2.通过restTemplate发起的Get、POST请求,可自定义请求头
使用本套api发起get/post请求只需要简单传参 url、请求头、请求体
- package x.bfb6.cn.bfb_km.utils;
-
- import com.alibaba.fastjson.JSONObject;
- import org.springframework.http.*;
- import org.springframework.web.client.RequestCallback;
- import org.springframework.web.client.ResponseExtractor;
- import org.springframework.web.client.RestTemplate;
-
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Objects;
-
- public class HttpGetAndPost {
-
- /**
- * 通用post请求封装
- * @param url
- * @param jsonData
- * @return
- */
- public String httpPost(String url, JSONObject jsonData){
- RestTemplate restTemplate = new RestTemplate();
- //设置Http Header
- HttpHeaders headers = new HttpHeaders();
- headers.add("Content-Type","application/json");
- headers.add("Accept","application/json, text/plain, */*");
- //设置请求媒体数据类型
- headers.setContentType(MediaType.APPLICATION_JSON);
- //设置返回媒体数据类型
- headers.add("Accept", MediaType.APPLICATION_JSON.toString());
- HttpEntity<String> formEntity = new HttpEntity<String>(jsonData.toString(), headers);
- return restTemplate.postForObject(url, formEntity, String.class);
- }
-
- /**
- * 通用get请求
- * @param url
- * @return
- */
- public String httpGet(String url){
- RestTemplate restTemplate = new RestTemplate();
- //设置Http Header
- HttpHeaders headers = new HttpHeaders();
- headers.add("Content-Type","application/json");
- headers.add("Accept","application/json, text/plain, */*");
- //设置请求媒体数据类型
- headers.setContentType(MediaType.APPLICATION_JSON);
- //设置返回媒体数据类型
- headers.add("Accept", MediaType.APPLICATION_JSON.toString());
- return restTemplate.getForObject(url, String.class);
- }
-
- /**
- * 自定义请求头的post请求
- * @param url
- * @param jsonData
- * @param jsonHeaders
- * @return
- */
- public String httpPost(String url, JSONObject jsonData,JSONObject jsonHeaders){
- RestTemplate restTemplate = new RestTemplate();
- //设置Http Header
- HttpHeaders headers = getHeaders(jsonHeaders);
- //设置请求媒体数据类型
- headers.setContentType(MediaType.APPLICATION_JSON);
- //设置返回媒体数据类型
- headers.add("Accept", MediaType.APPLICATION_JSON.toString());
- HttpEntity<String> formEntity = new HttpEntity<String>(jsonData.toString(), headers);
- return restTemplate.postForObject(url, formEntity, String.class);
- }
-
-
- /**
- * 自定义请求头的get请求
- * @param url
- * @param jsonHeaders
- * @return
- */
- public String httpGet(String url,JSONObject jsonHeaders){
- RestTemplate restTemplate = new RestTemplate();
- HttpHeaders headers = getHeaders(jsonHeaders);
- HttpEntity request = new HttpEntity(headers);
- // 构造execute()执行所需要的参数。
- RequestCallback requestCallback = restTemplate.httpEntityCallback(request, JSONObject.class);
- ResponseExtractor<ResponseEntity<JSONObject>> responseExtractor = restTemplate.responseEntityExtractor(JSONObject.class);
- // 执行execute(),发送请求
- return Objects.requireNonNull(Objects.requireNonNull(restTemplate.execute(url, HttpMethod.GET, requestCallback, responseExtractor)).getBody()).toString();
- }
-
-
-
-
- /**
- * json转化请求头
- * @param jsonHeaders
- * @return
- */
- public static HttpHeaders getHeaders(JSONObject jsonHeaders){
- Iterator iterator = jsonHeaders.entrySet().iterator();
- HttpHeaders headers = new HttpHeaders();
- while (iterator.hasNext()){
- Map.Entry entry = (Map.Entry) iterator.next();
- headers.add(entry.getKey().toString(),entry.getValue().toString());
- }
- return headers;
- }
-
-
- }
测试类
- package x.bfb6.cn.bfb_km;
-
-
- import com.alibaba.fastjson.JSONObject;
- import org.junit.jupiter.api.Test;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.http.HttpEntity;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.MediaType;
- import org.springframework.web.client.RestTemplate;
- import x.bfb6.cn.bfb_km.utils.HttpGetAndPost;
-
- import java.util.Iterator;
- import java.util.Map;
-
- @SpringBootTest
- public class HttpTest {
- HttpGetAndPost httpGetAndPost = new HttpGetAndPost();
-
- //测试get请求
- @Test
- public void getTest(){
- String data = httpGetAndPost.httpGet("http://localhost:8081/api/v1/pub/video/list");
- System.out.println("get请求成功");
- System.err.println(data);
- }
-
- //测试post请求
- @Test
- public void postTest(){
- String pwd ="6515551",phone = "651561";
- JSONObject jsonObject = new JSONObject();
- JSONObject jsonData = jsonObject.fluentPut("phone", phone).fluentPut("pwd", pwd);
- String data = httpGetAndPost.httpPost("http://localhost:8081/api/v1/pri/user/login",jsonData);
- System.out.println("post请求成功");
- System.err.println(data);
- }
-
-
- //测试get请求自定义请求头
- @Test
- public void getCustomHeaderTest(){
- String token ="xdclasseyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4ZGNsYXNzIiwiaGVhZF9pbWciOiJodHRwczovL3hkLXZpZGVvLXBjLWltZy5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20veGRjbGFzc19wcm8vZGVmYXVsdC9oZWFkX2ltZy8xNC5qcGVnIiwiaWQiOjMxLCJuYW1lIjoiNTY1NTYiLCJpYXQiOjE2NTM0NjI0NDYsImV4cCI6MTY1NDA2NzI0Nn0.VGXePtXixZEq28l-6AXk8C1dFsVBojHbKb_fJ1ArXrk";
- JSONObject jsonObject = new JSONObject();
- JSONObject jsonData = jsonObject.fluentPut("token", token);
- String data = httpGetAndPost.httpGet("http://localhost:8081/api/v1/pri/user/find_by_token",jsonData);
- System.out.println("get带请求头请求成功");
- System.err.println(data);
- }
-
- //测试get请求自定义请求头
- @Test
- public void postCustomHeaderTest(){
- String video_id ="40",token = "xdclasseyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ4ZGNsYXNzIiwiaGVhZF9pbWciOiJodHRwczovL3hkLXZpZGVvLXBjLWltZy5vc3MtY24tYmVpamluZy5hbGl5dW5jcy5jb20veGRjbGFzc19wcm8vZGVmYXVsdC9oZWFkX2ltZy8xNC5qcGVnIiwiaWQiOjMxLCJuYW1lIjoiNTY1NTYiLCJpYXQiOjE2NTM0NjI0NDYsImV4cCI6MTY1NDA2NzI0Nn0.VGXePtXixZEq28l-6AXk8C1dFsVBojHbKb_fJ1ArXrk";
- //设置请求体
- JSONObject jsonObject = new JSONObject();
- JSONObject jsonData = jsonObject.fluentPut("video_id", video_id);
- //设置请求头
- JSONObject jsonObject2 = new JSONObject();
- JSONObject jsonData2 = jsonObject2.fluentPut("token", token);
- String data = httpGetAndPost.httpPost("http://localhost:8081/api/v1/pri/order/save",jsonData,jsonData2);
- System.out.println("post带请求头请求成功");
- System.err.println(data);
- }
-
-
- @Test
- public void test(){
- getTest();
- postTest();
- getCustomHeaderTest();
- postCustomHeaderTest();
- }
-
- }
-
测试结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。