目录
1. @Controller
2. @RequestMapping
3. @GetMapping、@PostMapping、@PutMapping、@DeleteMapping
4. @RequestBody
5. @ResponseBody
6. @PathVariable
7. @RequestParam
8. @ModelAttribute
在 Spring MVC 项目中,注解是非常重要的组成部分,它可以帮助我们更简洁、高效地开发 Web 应用。以下是 Spring MVC 中常用注解的详细解释、用法及场景示例。
@Controller
@Component
的一种特殊形式,Spring 框架会自动扫描带有该注解的类,并将其注册为 Spring 容器中的 Bean。@Controller
注解。import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/world")
@ResponseBody
public String sayHello() {
return "Hello, World!";
}
}
在上述示例中,HelloController
类被标记为 @Controller
,表示它是一个控制器,负责处理 /hello
路径下的请求。
@RequestMapping
method
属性指定请求的 HTTP 方法(如 GET
、POST
等)。@RequestMapping
注解,并通过 value
属性指定请求路径,method
属性指定请求方法。import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public String getUserList() {
return "User List";
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public String addUser() {
return "User Added";
}
}
在这个示例中,UserController
类处理 /user
路径下的请求,getUserList
方法处理 /user/list
的 GET
请求,addUser
方法处理 /user/add
的 POST
请求。
@GetMapping
、@PostMapping
、@PutMapping
、@DeleteMapping
@RequestMapping
的特定形式,分别对应 GET
、POST
、PUT
、DELETE
请求方法,使用它们可以使代码更加简洁易读。import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/product")
public class ProductController {
@GetMapping("/list")
@ResponseBody
public String getProductList() {
return "Product List";
}
@PostMapping("/add")
@ResponseBody
public String addProduct() {
return "Product Added";
}
}
这里 getProductList
方法使用 @GetMapping
处理 /product/list
的 GET
请求,addProduct
方法使用 @PostMapping
处理 /product/add
的 POST
请求。
@RequestBody
@RequestBody
注解。import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/order")
public class OrderController {
@PostMapping("/create")
@ResponseBody
public String createOrder(@RequestBody Order order) {
// 处理订单创建逻辑
return "Order Created: " + order.getOrderNumber();
}
}
class Order {
private String orderNumber;
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
}
在这个示例中,createOrder
方法使用 @RequestBody
将请求体中的 JSON 数据转换为 Order
对象。
@ResponseBody
@ResponseBody
注解。import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/data")
public class DataController {
@GetMapping("/info")
@ResponseBody
public Map getDataInfo() {
Map data = new HashMap<>();
data.put("name", "John");
data.put("age", "30");
return data;
}
}
这里 getDataInfo
方法使用 @ResponseBody
直接将 Map
对象作为 JSON 数据返回给客户端。
@PathVariable
@PathVariable
注解,并指定变量名。import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/book")
public class BookController {
@GetMapping("/{id}")
@ResponseBody
public String getBookById(@PathVariable("id") String bookId) {
return "Book ID: " + bookId;
}
}
在这个示例中,getBookById
方法使用 @PathVariable
从 URL 路径 /book/{id}
中提取 id
的值,并将其绑定到 bookId
参数上。
@RequestParam
@RequestParam
注解,并指定参数名。import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/search")
public class SearchController {
@GetMapping("/books")
@ResponseBody
public String searchBooks(@RequestParam("keyword") String keyword) {
return "Searching for books with keyword: " + keyword;
}
}
这里 searchBooks
方法使用 @RequestParam
从请求参数中获取 keyword
的值。
@ModelAttribute
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/customer")
public class CustomerController {
@PostMapping("/register")
@ResponseBody
public String registerCustomer(@ModelAttribute Customer customer) {
// 处理客户注册逻辑
return "Customer Registered: " + customer.getName();
}
}
class Customer {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
@RequestMapping("/product")
public class ProductModelController {
@ModelAttribute
public void addProductInfo(Model model) {
model.addAttribute("productType", "Electronics");
}
@GetMapping("/info")
public String showProductInfo(Model model) {
// 这里可以使用 addProductInfo 方法添加到模型中的数据
return "productInfo";
}
}
在上述示例中,第一个示例将请求参数绑定到 Customer
对象,第二个示例在视图渲染前将 productType
添加到模型中。