feat: Spring Boot示例项目
- 基础Spring Boot配置 - Docker支持
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import com.example.demo.aop.PerformanceAspect;
|
||||
import com.example.demo.aop.RateLimitAspect;
|
||||
import com.example.demo.aop.RateLimited;
|
||||
import com.example.demo.event.UserEventPublisher;
|
||||
import com.example.demo.model.User;
|
||||
import com.example.demo.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* AOP 和事件机制学习控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/aop")
|
||||
public class AopEventController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private UserEventPublisher eventPublisher;
|
||||
|
||||
@Autowired
|
||||
private PerformanceAspect performanceAspect;
|
||||
|
||||
@Autowired
|
||||
private RateLimitAspect rateLimitAspect;
|
||||
|
||||
/**
|
||||
* 学习首页
|
||||
*/
|
||||
@GetMapping
|
||||
public Map<String, Object> index() {
|
||||
Map<String, Object> info = new HashMap<>();
|
||||
info.put("message", "Spring Boot 学习中心");
|
||||
info.put("topics", new String[]{
|
||||
"AOP 切面编程",
|
||||
"事件机制",
|
||||
"Bean 生命周期"
|
||||
});
|
||||
info.put("endpoints", new String[]{
|
||||
"GET /aop - AOP 概念说明",
|
||||
"GET /aop/stats - 性能统计",
|
||||
"GET /aop/event - 事件机制说明",
|
||||
"POST /aop/event/publish - 发布用户事件",
|
||||
"GET /aop/event/history - 查看事件历史",
|
||||
"GET /aop/ratelimit - 限流测试"
|
||||
});
|
||||
return info;
|
||||
}
|
||||
|
||||
// ==================== AOP 示例 ====================
|
||||
|
||||
/**
|
||||
* AOP 概念说明
|
||||
*/
|
||||
@GetMapping("/aop")
|
||||
public Map<String, Object> aopInfo() {
|
||||
Map<String, Object> info = new HashMap<>();
|
||||
info.put("title", "AOP 切面编程");
|
||||
info.put("concepts", new String[]{
|
||||
"Aspect(切面): 横切关注点的模块化",
|
||||
"JoinPoint(连接点): 程序执行的某个点",
|
||||
"Pointcut(切入点): 匹配连接点的表达式",
|
||||
"Advice(通知): 在连接点执行的动作",
|
||||
"Weaving(织入): 将切面应用到目标对象"
|
||||
});
|
||||
info.put("adviceTypes", new String[]{
|
||||
"@Before - 方法执行前",
|
||||
"@After - 方法执行后(无论成功或异常)",
|
||||
"@AfterReturning - 方法成功返回后",
|
||||
"@AfterThrowing - 方法抛出异常后",
|
||||
"@Around - 环绕通知(最强大)"
|
||||
});
|
||||
info.put("useCases", new String[]{
|
||||
"日志记录",
|
||||
"性能监控",
|
||||
"事务管理",
|
||||
"权限检查",
|
||||
"限流控制"
|
||||
});
|
||||
return info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看性能统计
|
||||
*/
|
||||
@GetMapping("/aop/stats")
|
||||
public Map<String, Object> getPerformanceStats() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("title", "方法性能统计");
|
||||
result.put("description", "由 PerformanceAspect 自动收集");
|
||||
result.put("statistics", performanceAspect.getStatistics());
|
||||
return result;
|
||||
}
|
||||
|
||||
// ==================== 事件机制示例 ====================
|
||||
|
||||
/**
|
||||
* 事件机制说明
|
||||
*/
|
||||
@GetMapping("/event")
|
||||
public Map<String, Object> eventInfo() {
|
||||
Map<String, Object> info = new HashMap<>();
|
||||
info.put("title", "Spring 事件机制");
|
||||
info.put("concepts", new String[]{
|
||||
"ApplicationEventPublisher - 事件发布者",
|
||||
"@EventListener - 事件监听器",
|
||||
"@Async - 异步处理事件",
|
||||
"condition - 条件过滤"
|
||||
});
|
||||
info.put("benefits", new String[]{
|
||||
"解耦:发布者和监听者互不依赖",
|
||||
"扩展:新增监听器无需修改发布者",
|
||||
"异步:耗时操作不阻塞主流程",
|
||||
"测试:更容易进行单元测试"
|
||||
});
|
||||
info.put("eventTypes", new String[]{
|
||||
"CREATED - 用户创建",
|
||||
"UPDATED - 用户更新",
|
||||
"DELETED - 用户删除",
|
||||
"LOGIN - 用户登录"
|
||||
});
|
||||
return info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布用户事件
|
||||
*/
|
||||
@PostMapping("/event/publish")
|
||||
public Map<String, Object> publishEvent(
|
||||
@RequestParam Long userId,
|
||||
@RequestParam String userName,
|
||||
@RequestParam(defaultValue = "LOGIN") String eventType
|
||||
) {
|
||||
eventPublisher.publishUserLogin(userId, userName);
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("message", "事件已发布");
|
||||
result.put("eventType", eventType);
|
||||
result.put("userId", userId);
|
||||
result.put("userName", userName);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看事件历史
|
||||
*/
|
||||
@GetMapping("/event/history")
|
||||
public Map<String, Object> getEventHistory() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("title", "事件历史记录");
|
||||
result.put("note", "由 UserEventListener 自动记录");
|
||||
return result;
|
||||
}
|
||||
|
||||
// ==================== 限流示例 ====================
|
||||
|
||||
/**
|
||||
* 限流测试接口
|
||||
*/
|
||||
@GetMapping("/ratelimit")
|
||||
@RateLimited(value = 10, message = "测试限流:每分钟最多10次")
|
||||
public Map<String, Object> testRateLimit() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("message", "请求成功");
|
||||
result.put("note", "使用 @RateLimited 注解");
|
||||
result.put("rateLimitStatus", rateLimitAspect.getRateLimitStatus());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
116
src/main/java/com/example/demo/controller/LearnController.java
Normal file
116
src/main/java/com/example/demo/controller/LearnController.java
Normal file
@@ -0,0 +1,116 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 学习示例控制器
|
||||
*
|
||||
* 学习点:
|
||||
* - 各种参数接收方式
|
||||
* - 配置注入
|
||||
* - 响应格式
|
||||
*/
|
||||
@RestController
|
||||
public class LearnController {
|
||||
|
||||
// 从配置文件注入值
|
||||
@Value("${spring.application.name:demo}")
|
||||
private String appName;
|
||||
|
||||
/**
|
||||
* 根路径 - 重定向到学习中心
|
||||
*/
|
||||
@GetMapping("/")
|
||||
public Map<String, Object> root() {
|
||||
Map<String, Object> info = new HashMap<>();
|
||||
info.put("message", "欢迎来到 Spring Boot 学习脚手架!");
|
||||
info.put("learn", "https://spring.xiaoxiaoluohao.indevs.in/learn");
|
||||
info.put("aop", "https://spring.xiaoxiaoluohao.indevs.in/aop");
|
||||
info.put("api", "https://spring.xiaoxiaoluohao.indevs.in/api/users");
|
||||
return info;
|
||||
}
|
||||
|
||||
// GET /learn - API 信息
|
||||
@GetMapping("/learn")
|
||||
public Map<String, Object> info() {
|
||||
Map<String, Object> info = new HashMap<>();
|
||||
info.put("app", appName);
|
||||
info.put("message", "欢迎学习 Spring Boot!");
|
||||
info.put("endpoints", new String[]{
|
||||
"GET /learn/params?name=xxx&age=18 - 参数示例",
|
||||
"POST /learn/body - JSON 请求体示例",
|
||||
"GET /learn/path/{id} - 路径变量示例",
|
||||
"GET /learn/header - 请求头示例",
|
||||
"GET /learn/cookie - Cookie 示例"
|
||||
});
|
||||
return info;
|
||||
}
|
||||
|
||||
// GET /learn/params?name=xxx&age=18 - 查询参数
|
||||
@GetMapping("/learn/params")
|
||||
public Map<String, Object> params(
|
||||
@RequestParam(required = false, defaultValue = "游客") String name,
|
||||
@RequestParam(required = false, defaultValue = "0") Integer age
|
||||
) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("name", name);
|
||||
result.put("age", age);
|
||||
result.put("tip", "使用 @RequestParam 接收查询参数");
|
||||
return result;
|
||||
}
|
||||
|
||||
// POST /learn/body - 请求体
|
||||
@PostMapping("/learn/body")
|
||||
public Map<String, Object> body(@RequestBody Map<String, Object> data) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("received", data);
|
||||
result.put("tip", "使用 @RequestBody 接收 JSON 请求体");
|
||||
return result;
|
||||
}
|
||||
|
||||
// GET /learn/path/{id} - 路径变量
|
||||
@GetMapping("/learn/path/{id}")
|
||||
public Map<String, Object> path(@PathVariable String id) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("id", id);
|
||||
result.put("tip", "使用 @PathVariable 接收路径变量");
|
||||
return result;
|
||||
}
|
||||
|
||||
// GET /learn/header - 请求头
|
||||
@GetMapping("/learn/header")
|
||||
public Map<String, Object> header(@RequestHeader(value = "User-Agent", required = false) String userAgent) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("userAgent", userAgent);
|
||||
result.put("tip", "使用 @RequestHeader 获取请求头");
|
||||
return result;
|
||||
}
|
||||
|
||||
// GET /learn/cookie - Cookie
|
||||
@GetMapping("/learn/cookie")
|
||||
public Map<String, Object> cookie(@CookieValue(value = "JSESSIONID", required = false) String sessionId) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("sessionId", sessionId);
|
||||
result.put("tip", "使用 @CookieValue 获取 Cookie");
|
||||
return result;
|
||||
}
|
||||
|
||||
// GET /learn/exception - 异常处理
|
||||
@GetMapping("/learn/exception")
|
||||
public String exception() {
|
||||
throw new RuntimeException("这是一个测试异常");
|
||||
}
|
||||
|
||||
// 全局异常处理
|
||||
@ExceptionHandler(RuntimeException.class)
|
||||
public Map<String, Object> handleException(RuntimeException e) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("error", e.getMessage());
|
||||
result.put("tip", "使用 @ExceptionHandler 处理异常");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
/**
|
||||
* 页面控制器 - 返回 HTML 页面
|
||||
*/
|
||||
@Controller
|
||||
public class PageController {
|
||||
|
||||
@GetMapping("/home")
|
||||
public String home() {
|
||||
return "forward:/index.html";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import com.example.demo.model.User;
|
||||
import com.example.demo.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户控制器 - RESTful API 示例
|
||||
*
|
||||
* 学习点:
|
||||
* - @RestController: 组合了 @Controller 和 @ResponseBody
|
||||
* - @RequestMapping: 路由映射
|
||||
* - @PathVariable: 路径变量
|
||||
* - @RequestParam: 查询参数
|
||||
* - @RequestBody: 请求体
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/users")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
// GET /api/users - 获取所有用户
|
||||
@GetMapping
|
||||
public List<User> getAllUsers() {
|
||||
return userService.findAll();
|
||||
}
|
||||
|
||||
// GET /api/users/{id} - 获取单个用户
|
||||
@GetMapping("/{id}")
|
||||
public User getUserById(@PathVariable Long id) {
|
||||
return userService.findById(id);
|
||||
}
|
||||
|
||||
// POST /api/users - 创建用户
|
||||
@PostMapping
|
||||
public User createUser(@RequestBody User user) {
|
||||
return userService.save(user);
|
||||
}
|
||||
|
||||
// PUT /api/users/{id} - 更新用户
|
||||
@PutMapping("/{id}")
|
||||
public User updateUser(@PathVariable Long id, @RequestBody User user) {
|
||||
user.setId(id);
|
||||
return userService.save(user);
|
||||
}
|
||||
|
||||
// DELETE /api/users/{id} - 删除用户
|
||||
@DeleteMapping("/{id}")
|
||||
public String deleteUser(@PathVariable Long id) {
|
||||
userService.delete(id);
|
||||
return "用户 " + id + " 已删除";
|
||||
}
|
||||
|
||||
// GET /api/users/search?name=xxx - 搜索用户
|
||||
@GetMapping("/search")
|
||||
public List<User> searchUsers(@RequestParam String name) {
|
||||
return userService.findByName(name);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user