赞
踩
GitHub API 是一组 RESTful API 接口,用于与 GitHub 平台进行交互。通过使用 GitHub API,开发人员可以访问和操作 GitHub 平台上的各种资源,如仓库、提交记录、问题等。
GitHub API 提供了多种功能和端点,以满足开发人员的需求。一些常用的功能包括:
GitHub API 使用标准的 HTTP 协议进行通信,并返回 JSON 格式的数据。开发人员可以使用任何支持 HTTP 请求的编程语言来与 GitHub API 进行交互。
https://api.github.com/users/{user} | 查询用户数据 |
https://api.github.com/users/{user}/repos | 查询仓库数据 |
https://api.github.com/repos/{user}/{repos} | 查询项目 |
以上接口可以填写具体参数,直接在浏览器进行访问。
工具名 | 作用 | maven依赖 |
httpclient | http查询 | <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.4</version> </dependency> |
jackson | json反序列化 | <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.1</version> </dependency> |
- private static <T> T queryBean(String url, Class<T> c) throws Exception {
- String json = httpGet(url);
- return JsonUtil.string2Object(json, c);
- }
-
- private static <C extends Collection<E>, E> E queryList(String url, Class<E> c) throws Exception {
- String json = httpGet(url);
- return (E) JsonUtil.string2Collection(json, ArrayList.class, c);
- }
-
- private static String httpGet(String url) throws IOException {
- CloseableHttpClient client = HttpClients.createDefault();
- CloseableHttpResponse response = client.execute(new HttpGet(url));
- HttpEntity httpEntity = response.getEntity();
- String json = EntityUtils.toString(httpEntity);
- return json;
- }
上述代码主要有两个查询函数,一个查询单个实体,一个查询实体列表。
需要注意的是,由于GitHub API返回的查询结果数据非常全面,但我们的代码可能只需要部分字段(需要什么字段自行添加),在使用jackson进行反序列化的时候,应该设置忽略没有申明的字段。
- import com.fasterxml.jackson.annotation.JsonAutoDetect;
- import com.fasterxml.jackson.annotation.JsonInclude;
- import com.fasterxml.jackson.annotation.PropertyAccessor;
- import com.fasterxml.jackson.databind.DeserializationFeature;
- import com.fasterxml.jackson.databind.JavaType;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.fasterxml.jackson.databind.type.ArrayType;
- import com.fasterxml.jackson.databind.type.TypeFactory;
-
- import java.io.StringWriter;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Map;
-
- public final class JsonUtil {
-
- private static TypeFactory typeFactory = TypeFactory.defaultInstance();
-
- private static final ObjectMapper MAPPER = new ObjectMapper();
-
-
- static {
- MAPPER.setSerializationInclusion(JsonInclude.Include.NON_NULL);
- MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
- MAPPER.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
- MAPPER.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
- MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
- }
-
- public static String object2String(Object object) {
- StringWriter writer = new StringWriter();
- try {
- MAPPER.writeValue(writer, object);
- }catch(Exception e) {
- return null;
- }
- return writer.toString();
- }
-
- @SuppressWarnings("unchecked")
- public static <T> T string2Object(String json, Class<T> clazz) {
- JavaType type = typeFactory.constructType(clazz);
- try {
- return (T) MAPPER.readValue(json, type);
- } catch(Exception e) {
- return null;
- }
- }
-
- public static String map2String(Map<?,?> map) {
- StringWriter writer = new StringWriter();
- try {
- MAPPER.writeValue(writer, map);
- } catch(Exception e) {
- return null;
- }
- return writer.toString();
- }
-
- public static Map<String, Object> string2Map(String json) {
- JavaType type = typeFactory.constructMapType(HashMap.class, String.class, Object.class);
- try {
- return MAPPER.readValue(json, type);
- } catch(Exception e) {
- return null;
- }
- }
-
- public static <K, V> Map<K, V> string2Map(String json, Class<K> keyClazz, Class<V> valueClazz) {
- JavaType type = typeFactory.constructMapType(HashMap.class, keyClazz, valueClazz);
- try {
- return MAPPER.readValue(json, type);
- } catch(Exception e) {
- return null;
- }
- }
-
- @SuppressWarnings("unchecked")
- public static <T> T[] string2Array(String json, Class<T> clazz) {
- ArrayType type = typeFactory.constructArrayType(clazz);
- try {
- return (T[]) MAPPER.readValue(json, type);
- } catch(Exception e) {
- return null;
- }
- }
-
- public static <C extends Collection<E>, E> C string2Collection(String json, Class<C> collectionType,
- Class<E> elemType) {
- JavaType type = typeFactory.constructCollectionType(collectionType, elemType);
- try {
- return MAPPER.readValue(json, type);
- } catch(Exception e) {
- return null;
- }
- }
-
- }
这里使用了lombok简化bean编写
- import lombok.Data;
-
- @Data
- public class ProjectView {
-
- private String html_url;
-
- private boolean fork;
-
- private String description;
-
- // 开发语言
- private String language;
-
- // 点赞数
- private int stargazers_count;
-
- // fork数
- private int forks_count;
-
- }
- import lombok.Data;
-
- @Data
- public class UserView {
-
- // github主页
- private String html_url;
-
- // 公有仓库数量
- private int public_repos;
-
- private int followers;
- }
- public static void main(String[] args) throws Exception {
- String user = "kingston-csj";
- String project = "jforgame";
- // 查询用户数据
- String url1 = "https://api.github.com/users/{username}".replace("{username}", user);
- System.out.println(queryBean(url1, UserView.class));
- // 查询仓库数据
- String url2 = "https://api.github.com/users/{username}/repos".replace("{username}", user);
- System.out.println(queryList(url2, ProjectView.class));
- // 查询具体仓库
- String url3 = "https://api.github.com/repos/{username}/{reposname}".replace("{username}", user).replace("{reposname}",project);
- System.out.println(queryBean(url3, ProjectView.class));
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。