@Controller @RequestMapping("/users") public class UserController {
// 实际路径为 /users/profile
@RequestMapping("/profile")
public String showProfile() {
return "userProfile";
}
}
@RestController public class ProductController {
@GetMapping("/products")
public List<Product> getAllProducts() {
return productService.findAll();
}
@GetMapping("/products/{id}")
public Product getProductById(@PathVariable Long id) {
return productService.findById(id);
}
}

@GetMapping("/search")
public List
@RequestParam String keyword,
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "10") int size) {
return productService.search(keyword, page, size);
}

@GetMapping("/users/{userId}") public User getUser(@PathVariable Long userId) {
return userService.findById(userId);
}
@GetMapping("/products/{category}/{id}") public Product getProduct(@PathVariable String category,
@PathVariable Long id) {
return productService.findByCategoryAndId(category, id);
}
你可能想看: