Java直通车系列13【Spring MVC】(Spring MVC常用注解)
目录
1. @Controller
2. @RequestMapping
3. @GetMapping、@PostMapping、@PutMapping、@DeleteMapping
4. @RequestBody
5. @ResponseBody
6. @PathVariable
7. @RequestParam
8. @ModelAttribute
在 Spring MVC 项目中,注解是非常重要的组成部分,它可以帮助我们更简洁、高效地开发 Web 应用。以下是 Spring MVC 中常用注解的详细解释、用法及场景示例。
1. @Controller
- 解释:这是一个用于标记类的注解,表明该类是一个 Spring MVC 的控制器,负责处理客户端的请求。它是
@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
路径下的请求。
2. @RequestMapping
- 解释:用于将 HTTP 请求映射到控制器的处理方法上。可以在类和方法上使用,在类上使用时指定该控制器处理的基础请求路径,在方法上使用时指定具体的请求路径。还可以通过
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
请求。
3. @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
请求。
4. @RequestBody
- 解释:用于将 HTTP 请求的主体内容绑定到方法的参数上,通常用于处理 JSON 或 XML 格式的数据。Spring 会自动将请求体中的数据转换为方法参数的类型。
- 用法:在方法的参数前添加
@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
对象。
5. @ResponseBody
- 解释:用于将方法的返回值直接作为 HTTP 响应的主体内容返回给客户端,而不是将其解析为视图名。通常用于返回 JSON 或 XML 数据。
- 用法:在方法上添加
@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<String, String> getDataInfo() {
Map<String, String> data = new HashMap<>();
data.put("name", "John");
data.put("age", "30");
return data;
}
}
这里 getDataInfo
方法使用 @ResponseBody
直接将 Map
对象作为 JSON 数据返回给客户端。
6. @PathVariable
- 解释:用于从 URL 路径中提取变量值,并将其绑定到方法的参数上。通常用于 RESTful 风格的 URL。
- 用法:在方法的参数前添加
@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
参数上。
7. @RequestParam
- 解释:用于从 HTTP 请求的参数中获取值,并将其绑定到方法的参数上。可以指定参数名、是否必需、默认值等。
- 用法:在方法的参数前添加
@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
的值。
8. @ModelAttribute
- 解释:有两种主要用法,一是用于将请求参数绑定到一个 Java 对象上,二是用于在视图渲染前将数据添加到模型中。
- 用法:
- 作为方法参数注解,将请求参数绑定到对象:
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
添加到模型中。