当前位置:首页 > Java 框架原理百科 > 正文

Java优学网SpringMVC请求映射讲解:轻松掌握URL路径配置与参数处理技巧

@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);
}

}

Java优学网SpringMVC请求映射讲解:轻松掌握URL路径配置与参数处理技巧

@GetMapping("/search") public List searchProducts(

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

}

Java优学网SpringMVC请求映射讲解:轻松掌握URL路径配置与参数处理技巧

@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);

}

你可能想看:

相关文章:

文章已关闭评论!