diff --git a/pom.xml b/pom.xml
index 907a57c..06bfb3b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -89,6 +89,13 @@
jackson-databind
2.15.3
+
+
+ jakarta.servlet
+ jakarta.servlet-api
+ 6.0.0
+ provided
+
diff --git a/src/main/java/com/cloverta/webapi/controller/BiliApiController.java b/src/main/java/com/cloverta/webapi/controller/BiliApiController.java
index e87cfcc..d3ace3b 100644
--- a/src/main/java/com/cloverta/webapi/controller/BiliApiController.java
+++ b/src/main/java/com/cloverta/webapi/controller/BiliApiController.java
@@ -2,10 +2,13 @@ package com.cloverta.webapi.controller;
import com.cloverta.webapi.restservice.BiliApi;
import com.cloverta.webapi.service.BiliApiService;
+import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
+@ResponseStatus(HttpStatus.OK)
@RestController
public class BiliApiController {
private final BiliApiService biliApiService;
diff --git a/src/main/java/com/cloverta/webapi/controller/CustomErrorController.java b/src/main/java/com/cloverta/webapi/controller/CustomErrorController.java
index 592459f..465819c 100644
--- a/src/main/java/com/cloverta/webapi/controller/CustomErrorController.java
+++ b/src/main/java/com/cloverta/webapi/controller/CustomErrorController.java
@@ -1,14 +1,20 @@
package com.cloverta.webapi.controller;
import com.cloverta.webapi.restservice.Error;
+import jakarta.servlet.http.HttpServletRequest;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomErrorController implements ErrorController {
@RequestMapping("/error")
- public Error error() {
- return new Error("ERROR", "Something went wrong... I hope it wasn't my fault.");
+ public Error error(HttpServletRequest request) {
+ String message = (String) request.getAttribute("javax.servlet.error.message");
+ if (message == null) {
+ message = "Something went wrong...";
+ }
+ return new Error("ERROR", message);
}
}
diff --git a/src/main/java/com/cloverta/webapi/controller/GithubStarsController.java b/src/main/java/com/cloverta/webapi/controller/GithubStarsController.java
new file mode 100644
index 0000000..0a87498
--- /dev/null
+++ b/src/main/java/com/cloverta/webapi/controller/GithubStarsController.java
@@ -0,0 +1,28 @@
+package com.cloverta.webapi.controller;
+
+import com.cloverta.webapi.exception.GithubException;
+import com.cloverta.webapi.restservice.GithubStars;
+import com.cloverta.webapi.service.GithubStarsService;
+import com.cloverta.webapi.service.GreetingService;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class GithubStarsController {
+
+ private final GithubStarsService githubStarsService;
+
+ public GithubStarsController(GithubStarsService githubStarsService) {
+ this.githubStarsService = githubStarsService;
+ }
+
+ @GetMapping("/github/stars")
+ public GithubStars getGithubStars(@RequestParam(value = "user", required = true) String user) {
+ try {
+ return githubStarsService.getGithubStars(user);
+ } catch (Exception e) {
+ throw new GithubException(e.getMessage());
+ }
+ }
+}
diff --git a/src/main/java/com/cloverta/webapi/exception/GithubException.java b/src/main/java/com/cloverta/webapi/exception/GithubException.java
new file mode 100644
index 0000000..a99c00a
--- /dev/null
+++ b/src/main/java/com/cloverta/webapi/exception/GithubException.java
@@ -0,0 +1,7 @@
+package com.cloverta.webapi.exception;
+
+public class GithubException extends RuntimeException {
+ public GithubException(String message) {
+ super(message);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/cloverta/webapi/handler/GlobalExceptionHandler.java b/src/main/java/com/cloverta/webapi/handler/GlobalExceptionHandler.java
new file mode 100644
index 0000000..73585a0
--- /dev/null
+++ b/src/main/java/com/cloverta/webapi/handler/GlobalExceptionHandler.java
@@ -0,0 +1,23 @@
+package com.cloverta.webapi.handler;
+
+import com.cloverta.webapi.exception.GithubException;
+import jakarta.servlet.http.HttpServletRequest;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+
+
+@ControllerAdvice
+public class GlobalExceptionHandler {
+
+ @ExceptionHandler(GithubException.class)
+ public String handleGithubException(GithubException ex, HttpServletRequest request) {
+ request.setAttribute("javax.servlet.error.message", ex.getMessage());
+ return "forward:/error";
+ }
+
+ @ExceptionHandler(Exception.class)
+ public String handleGeneralException(Exception ex, HttpServletRequest request) {
+ request.setAttribute("javax.servlet.error.message", ex.getMessage());
+ return "forward:/error";
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/cloverta/webapi/restservice/GithubStars.java b/src/main/java/com/cloverta/webapi/restservice/GithubStars.java
new file mode 100644
index 0000000..51844b0
--- /dev/null
+++ b/src/main/java/com/cloverta/webapi/restservice/GithubStars.java
@@ -0,0 +1,4 @@
+package com.cloverta.webapi.restservice;
+
+public record GithubStars(String status, String message, int stars) {
+}
diff --git a/src/main/java/com/cloverta/webapi/service/BiliApiService.java b/src/main/java/com/cloverta/webapi/service/BiliApiService.java
index f673f6e..2d1a4e1 100644
--- a/src/main/java/com/cloverta/webapi/service/BiliApiService.java
+++ b/src/main/java/com/cloverta/webapi/service/BiliApiService.java
@@ -51,7 +51,7 @@ public class BiliApiService {
JSONObject root = new JSONObject(jsonResponse);
JSONObject data;
- data = root.getJSONObject("data");
+ data = root.getJSONObject("data");
String bvId = data.getString("bvid");
String name = data.getString("title");
diff --git a/src/main/java/com/cloverta/webapi/service/GithubStarsService.java b/src/main/java/com/cloverta/webapi/service/GithubStarsService.java
new file mode 100644
index 0000000..9fcd448
--- /dev/null
+++ b/src/main/java/com/cloverta/webapi/service/GithubStarsService.java
@@ -0,0 +1,95 @@
+package com.cloverta.webapi.service;
+
+import com.cloverta.webapi.model.BiliVid;
+import com.cloverta.webapi.restservice.GithubStars;
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.springframework.stereotype.Service;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+
+@Service
+public class GithubStarsService {
+ public GithubStars generateGithubStars(boolean status, String message, int stars){
+ if (status) {
+ return new GithubStars("OK", message, stars);
+ }else {
+ return new GithubStars("ERROR", message, stars);
+ }
+ }
+
+ public GithubStars getGithubStars(String userName) {
+ HttpURLConnection connection = null;
+ InputStream is = null;
+ BufferedReader br = null;
+ StringBuilder result = new StringBuilder();
+ try {
+ //创建连接
+ URL url = new URL(String.format("https://api.github.com/users/%s/repos", userName));
+ connection = (HttpURLConnection) url.openConnection();
+ //设置请求方式
+ connection.setRequestMethod("GET");
+ //设置连接超时时间
+ connection.setReadTimeout(15000);
+ //开始连接
+ connection.connect();
+ //获取响应数据
+ if (connection.getResponseCode() == 200) {
+ //获取返回的数据
+ is = connection.getInputStream();
+ if (null != is) {
+ br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
+ String temp = null;
+ while (null != (temp = br.readLine())) {
+ result.append(temp);
+ }
+ }
+ }
+
+ String jsonResponse = result.toString();
+ JSONArray root = new JSONArray(jsonResponse);
+
+ int stars = 0;
+
+ for (Object info : root){
+ JSONObject obj = (JSONObject) info;
+ if (!obj.getBoolean("fork")){
+ stars += obj.getInt("stargazers_count");
+ }
+ }
+
+ return generateGithubStars(true, "success", stars);
+ } catch (IOException e) {
+ e.printStackTrace();
+ return generateGithubStars(false, "Cant access data: https://api.github.com unreachable. Please contact cloverta@petalmail.com for further support.", -1);
+ }catch (org.json.JSONException e) {
+ e.printStackTrace();
+ return generateGithubStars(false, "Invalid User Name: Please check whether there was a typo or whether the user exists.", -1);
+ } finally {
+ if (null != br) {
+ try {
+ br.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if (null != is) {
+ try {
+ is.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ //关闭远程连接
+ if (connection != null) {
+ connection.disconnect();
+ }
+ }
+ }
+}