Java17+Spring RestTemplate调用GithubApi
## 简介 GithubApi 官方提供一了一些SDK,node仓库的好用一些,Java的也有官方推
渲染中...
## 简介
GithubApi 官方提供一了一些SDK,node仓库的好用一些,Java的也有官方推荐的,但是用的不是很多,所以还是决定自己写以下。
本文记录几个使用 `RestTemplate` 调用 `GithubApi` 的实例。
- 环境:`Java17` + `Springboot3.2+`
<!-- more -->
## 输出List:分页请求
```java
/**
* 仓库发布信息列表查询URL
*/
private static final String RELEASE_LIST_URL = "https://api.github.com/repos/{user}/{repo}/releases";
/**
* 获取仓库Releases列表
*
* @param user user
* @param repo repo
* @param pageParam 分页参数
* @return List<GithubRelease>
*/
public static List<GithubRelease> getReleases(String user, String repo, Map<String, Object> pageParam) {
String url = RELEASE_LIST_URL
.replace("{user}", user)
.replace("{repo}", repo);
RestTemplate restTemplate = new RestTemplate();
// 请求信息封装
RequestEntity<Void> requestEntity = getRequestEntity(url, null, pageParam);
// 发送请求并获得响应
ResponseEntity<List<GithubRelease>> responseEntity = restTemplate.exchange(requestEntity, new ParameterizedTypeReference<List<GithubRelease>>() {
});
return responseEntity.getBody();
}
private static RequestEntity<Void> getRequestEntity(String url, Map<String, String> headerMap, Map<String, Object> param) {
HttpHeaders headers = new HttpHeaders();
if (AitLogProperties.getGithubToken() != null) {
headers.setBearerAuth(AitLogProperties.getGithubToken());
}
if (headerMap != null) {
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
headers.add(entry.getKey(), entry.getValue());
}
}
return new RequestEntity<>(headers, HttpMethod.GET, URI.create(url + mapToUrlParam(param)));
}
```
## 输出Map/Object
```java
/**
* 仓库基本信息查询URL
*/
private static final String REPO_INFO_URL = "https://api.github.com/repos/{user}/{repo}";
/**
* 获取仓库信息
*
* @param user GithubUser
* @param repo GithubRepo
* @return GithubRepository
*/
public static GithubRepository getRepoInfo(String user, String repo) {
String url = REPO_INFO_URL
.replace("{user}", user)
.replace("{repo}", repo);
return get(url, null, GithubRepository.class);
}
/**
* Get 请求封装
*
* @param url 请求地址
* @param param 请求参数
* @param clazz 响应数据实体类型
* @return 响应数据实体
*/
public static <T> T get(String url, Map<String, Object> param, Class<T> clazz) {
RestTemplate restTemplate = new RestTemplate();
RequestEntity<Void> requestEntity = getRequestEntity(url, null, param);
ResponseEntity<T> responseEntity = restTemplate.exchange(
requestEntity,
clazz
);
return responseEntity.getBody();
}
```
## 输出String
```java
/**
* Readme URL
*/
private static final String README_URL = "https://api.github.com/repos/{user}/{repo}/readme";
/**
* 获取一个仓库的Readme内容,以Html格式返回
*
* @param user the username of the repository owner
* @param repo the name of the repository
* @return the HTML content of the README file
*/
public static String getReadmeHtml(String user, String repo) {
String url = README_URL
.replace("{user}", user)
.replace("{repo}", repo);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
// 设置请求头
if (AitLogProperties.getGithubToken() != null) {
headers.setBearerAuth(AitLogProperties.getGithubToken());
}
headers.setAccept(MediaType.parseMediaTypes("application/vnd.github.html+json"));
RequestEntity<Void> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, URI.create(url));
// 发送请求并获得响应
ResponseEntity<String> responseEntity = restTemplate.exchange(
requestEntity,
String.class
);
return responseEntity.getBody();
}
```
END
评论
登录后查看和发表评论
前往登录