diff --git a/src/main/java/com/cloverta/webapi/WebapiApplication.java b/src/main/java/com/cloverta/webapi/WebapiApplication.java index 02a5c80..ac93964 100644 --- a/src/main/java/com/cloverta/webapi/WebapiApplication.java +++ b/src/main/java/com/cloverta/webapi/WebapiApplication.java @@ -13,6 +13,7 @@ public class WebapiApplication { String home(){ return "Welcome to Cloverta's WebAPI✨, please visit https://cloverta.top for more information."; } + public static void main(String[] args) { SpringApplication.run(WebapiApplication.class, args); } diff --git a/src/main/java/com/cloverta/webapi/controller/DemoController.java b/src/main/java/com/cloverta/webapi/controller/DemoController.java new file mode 100644 index 0000000..784abcc --- /dev/null +++ b/src/main/java/com/cloverta/webapi/controller/DemoController.java @@ -0,0 +1,26 @@ +package com.cloverta.webapi.controller; + +import com.cloverta.webapi.mapper.StudentMapper; +import com.cloverta.webapi.model.Student; +import com.cloverta.webapi.service.StudentService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/demo") +public class DemoController { + private StudentService studentService; + @Autowired + public void setStudentService(StudentService studentService) { + this.studentService = studentService; + } + + @GetMapping("/fetch") + public List fetch() { + return studentService.findAll(); + } +} diff --git a/src/main/java/com/cloverta/webapi/controller/GithubStarsController.java b/src/main/java/com/cloverta/webapi/controller/GithubStarsController.java index 0a87498..5c8ef56 100644 --- a/src/main/java/com/cloverta/webapi/controller/GithubStarsController.java +++ b/src/main/java/com/cloverta/webapi/controller/GithubStarsController.java @@ -5,10 +5,12 @@ 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.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController +@RequestMapping("/github") public class GithubStarsController { private final GithubStarsService githubStarsService; @@ -17,7 +19,7 @@ public class GithubStarsController { this.githubStarsService = githubStarsService; } - @GetMapping("/github/stars") + @GetMapping("/stars") public GithubStars getGithubStars(@RequestParam(value = "user", required = true) String user) { try { return githubStarsService.getGithubStars(user); diff --git a/src/main/java/com/cloverta/webapi/controller/GreetingController.java b/src/main/java/com/cloverta/webapi/controller/GreetingController.java index 3c1935b..7b1230f 100644 --- a/src/main/java/com/cloverta/webapi/controller/GreetingController.java +++ b/src/main/java/com/cloverta/webapi/controller/GreetingController.java @@ -14,7 +14,6 @@ public class GreetingController { private final GreetingService greetingService; - // 显式声明构造方法(Spring 4.3+ 可省略 @Autowired) public GreetingController(GreetingService greetingService) { this.greetingService = greetingService; } diff --git a/src/main/java/com/cloverta/webapi/handler/GlobalExceptionHandler.java b/src/main/java/com/cloverta/webapi/handler/GlobalExceptionHandler.java index 73585a0..8262094 100644 --- a/src/main/java/com/cloverta/webapi/handler/GlobalExceptionHandler.java +++ b/src/main/java/com/cloverta/webapi/handler/GlobalExceptionHandler.java @@ -2,6 +2,7 @@ package com.cloverta.webapi.handler; import com.cloverta.webapi.exception.GithubException; import jakarta.servlet.http.HttpServletRequest; +import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -12,11 +13,13 @@ public class GlobalExceptionHandler { @ExceptionHandler(GithubException.class) public String handleGithubException(GithubException ex, HttpServletRequest request) { request.setAttribute("javax.servlet.error.message", ex.getMessage()); + request.setAttribute("javax.servlet.error.status_code", HttpStatus.BAD_REQUEST.value()); return "forward:/error"; } @ExceptionHandler(Exception.class) public String handleGeneralException(Exception ex, HttpServletRequest request) { + request.setAttribute("javax.servlet.error.status_code", HttpStatus.BAD_REQUEST.value()); request.setAttribute("javax.servlet.error.message", ex.getMessage()); return "forward:/error"; } diff --git a/src/main/java/com/cloverta/webapi/mapper/ApiMapper.java b/src/main/java/com/cloverta/webapi/mapper/ApiMapper.java index 609bd3e..59e1945 100644 --- a/src/main/java/com/cloverta/webapi/mapper/ApiMapper.java +++ b/src/main/java/com/cloverta/webapi/mapper/ApiMapper.java @@ -10,7 +10,7 @@ import java.util.List; @Mapper public interface ApiMapper { - @Select("SELECT * FROM api_list") + List findAll(); } diff --git a/src/main/java/com/cloverta/webapi/mapper/StudentMapper.java b/src/main/java/com/cloverta/webapi/mapper/StudentMapper.java new file mode 100644 index 0000000..fd5de34 --- /dev/null +++ b/src/main/java/com/cloverta/webapi/mapper/StudentMapper.java @@ -0,0 +1,12 @@ +package com.cloverta.webapi.mapper; + +import com.cloverta.webapi.model.Student; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +@Mapper +public interface StudentMapper { + + List findAll(); +} diff --git a/src/main/java/com/cloverta/webapi/model/Student.java b/src/main/java/com/cloverta/webapi/model/Student.java new file mode 100644 index 0000000..33a0df3 --- /dev/null +++ b/src/main/java/com/cloverta/webapi/model/Student.java @@ -0,0 +1,41 @@ +package com.cloverta.webapi.model; + +public class Student { + public String id; + public String name; + public String email; + public String phone; + + public String getId() { + + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } +} diff --git a/src/main/java/com/cloverta/webapi/restservice/Student.java b/src/main/java/com/cloverta/webapi/restservice/Student.java new file mode 100644 index 0000000..0e6b8ce --- /dev/null +++ b/src/main/java/com/cloverta/webapi/restservice/Student.java @@ -0,0 +1,4 @@ +package com.cloverta.webapi.restservice; + +public record Student(String id, String name, String phone, String email) { +} diff --git a/src/main/java/com/cloverta/webapi/service/StudentService.java b/src/main/java/com/cloverta/webapi/service/StudentService.java new file mode 100644 index 0000000..61221b7 --- /dev/null +++ b/src/main/java/com/cloverta/webapi/service/StudentService.java @@ -0,0 +1,19 @@ +package com.cloverta.webapi.service; + +import com.cloverta.webapi.mapper.StudentMapper; +import com.cloverta.webapi.model.Student; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class StudentService { + private final StudentMapper studentMapper; + public StudentService(StudentMapper studentMapper) { + this.studentMapper = studentMapper; + } + + public List findAll() { + return studentMapper.findAll(); + } +} diff --git a/src/main/resources/mapper/ApiMapper.xml b/src/main/resources/mapper/ApiMapper.xml new file mode 100644 index 0000000..809af02 --- /dev/null +++ b/src/main/resources/mapper/ApiMapper.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/src/main/resources/mapper/StudentMapper.xml b/src/main/resources/mapper/StudentMapper.xml new file mode 100644 index 0000000..78e03ec --- /dev/null +++ b/src/main/resources/mapper/StudentMapper.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file