feat(v1): unify API response, validation, exception handling and tests
This commit is contained in:
@@ -6,7 +6,6 @@ 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;
|
||||
@@ -20,17 +19,20 @@ import java.util.Map;
|
||||
@RequestMapping("/aop")
|
||||
public class AopEventController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
private final UserService userService;
|
||||
private final UserEventPublisher eventPublisher;
|
||||
private final PerformanceAspect performanceAspect;
|
||||
private final RateLimitAspect rateLimitAspect;
|
||||
|
||||
@Autowired
|
||||
private UserEventPublisher eventPublisher;
|
||||
|
||||
@Autowired
|
||||
private PerformanceAspect performanceAspect;
|
||||
|
||||
@Autowired
|
||||
private RateLimitAspect rateLimitAspect;
|
||||
public AopEventController(UserService userService,
|
||||
UserEventPublisher eventPublisher,
|
||||
PerformanceAspect performanceAspect,
|
||||
RateLimitAspect rateLimitAspect) {
|
||||
this.userService = userService;
|
||||
this.eventPublisher = eventPublisher;
|
||||
this.performanceAspect = performanceAspect;
|
||||
this.rateLimitAspect = rateLimitAspect;
|
||||
}
|
||||
|
||||
/**
|
||||
* 学习首页
|
||||
|
||||
@@ -1,64 +1,54 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import com.example.demo.common.ApiResponse;
|
||||
import com.example.demo.dto.UserRequest;
|
||||
import com.example.demo.model.User;
|
||||
import com.example.demo.service.UserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import jakarta.validation.Valid;
|
||||
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;
|
||||
private final UserService userService;
|
||||
|
||||
public UserController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
// GET /api/users - 获取所有用户
|
||||
@GetMapping
|
||||
public List<User> getAllUsers() {
|
||||
return userService.findAll();
|
||||
public ApiResponse<List<User>> getAllUsers() {
|
||||
return ApiResponse.ok(userService.findAll());
|
||||
}
|
||||
|
||||
// GET /api/users/{id} - 获取单个用户
|
||||
@GetMapping("/{id}")
|
||||
public User getUserById(@PathVariable Long id) {
|
||||
return userService.findById(id);
|
||||
public ApiResponse<User> getUserById(@PathVariable Long id) {
|
||||
return ApiResponse.ok(userService.findById(id));
|
||||
}
|
||||
|
||||
// POST /api/users - 创建用户
|
||||
@PostMapping
|
||||
public User createUser(@RequestBody User user) {
|
||||
return userService.save(user);
|
||||
public ApiResponse<User> createUser(@Valid @RequestBody UserRequest req) {
|
||||
User user = new User(null, req.name(), req.email(), req.age());
|
||||
return ApiResponse.ok("创建成功", userService.create(user));
|
||||
}
|
||||
|
||||
// PUT /api/users/{id} - 更新用户
|
||||
@PutMapping("/{id}")
|
||||
public User updateUser(@PathVariable Long id, @RequestBody User user) {
|
||||
user.setId(id);
|
||||
return userService.save(user);
|
||||
public ApiResponse<User> updateUser(@PathVariable Long id, @Valid @RequestBody UserRequest req) {
|
||||
User user = new User(id, req.name(), req.email(), req.age());
|
||||
return ApiResponse.ok("更新成功", userService.update(id, user));
|
||||
}
|
||||
|
||||
// DELETE /api/users/{id} - 删除用户
|
||||
@DeleteMapping("/{id}")
|
||||
public String deleteUser(@PathVariable Long id) {
|
||||
public ApiResponse<Void> deleteUser(@PathVariable Long id) {
|
||||
userService.delete(id);
|
||||
return "用户 " + id + " 已删除";
|
||||
return ApiResponse.ok("删除成功", null);
|
||||
}
|
||||
|
||||
// GET /api/users/search?name=xxx - 搜索用户
|
||||
@GetMapping("/search")
|
||||
public List<User> searchUsers(@RequestParam String name) {
|
||||
return userService.findByName(name);
|
||||
public ApiResponse<List<User>> searchUsers(@RequestParam String name) {
|
||||
return ApiResponse.ok(userService.findByName(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user