TestMain.java
1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package org.jeecg;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.util.RestUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
/**
* @Description: TODO
* @author: scott
* @date: 2022年05月10日 14:02
*/
public class TestMain {
public static void main(String[] args) {
// 请求地址
String url = "https://api.boot.jeecg.com/sys/user/list";
// 请求 Header (用于传递Token)
HttpHeaders headers = getHeaders();
// 请求方式是 GET 代表获取数据
HttpMethod method = HttpMethod.GET;
System.out.println("请求地址:" + url);
System.out.println("请求方式:" + method);
// 利用 RestUtil 请求该url
ResponseEntity<JSONObject> result = RestUtil.request(url, method, headers, null, null, JSONObject.class);
if (result != null && result.getBody() != null) {
System.out.println("返回结果:" + result.getBody().toJSONString());
} else {
System.out.println("查询失败");
}
}
private static HttpHeaders getHeaders() {
String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.50h-g6INOZRVnznExiawFb1U6PPjcVVA4POeYRA5a5Q";
System.out.println("请求Token:" + token);
HttpHeaders headers = new HttpHeaders();
String mediaType = MediaType.APPLICATION_JSON_VALUE;
headers.setContentType(MediaType.parseMediaType(mediaType));
headers.set("Accept", mediaType);
headers.set("X-Access-Token", token);
return headers;
}
}