目录
1. 注解
1.1. 控制器相关注解
@Controller
作用:用于标记一个类为 Spring MVC 控制器,该类可以处理 HTTP 请求。
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@RequestMapping("/hello")
@ResponseBody
public String hello() {
return "Hello, Spring MVC!";
}
}
@RestController
作用:是@Controller
和 @ResponseBody
的组合注解,标记的类中所有方法返回的对象会自动序列化为 JSON
或 XML
格式响应给客户端。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@GetMapping("/user")
public User getUser() {
return new User("John", 25);
}
}
1.2. 请求映射注解
@RequestMapping
作用:是一个通用的请求映射注解,可以处理多种 HTTP 请求方法。可以指定请求路径、请求方法、请求参数等。
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/products")
public class ProductController {
@RequestMapping(method = RequestMethod.GET)
public String getProducts() {
return "List of products";
}
}
@GetMapping
作用:是 @RequestMapping(method = RequestMethod.GET) 的快捷方式,专门处理 HTTP GET 请求。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BookController {
@GetMapping("/books")
public String getBooks() {
return "List of books";
}
}
@PostMapping
作用:是 @RequestMapping(method = RequestMethod.POST) 的快捷方式,专门处理 HTTP POST 请求。
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@PostMapping("/orders")
public String createOrder(@RequestBody Order order) {
return "Order created successfully";
}
}
@PutMapping
作用:是 @RequestMapping(method = RequestMethod.PUT) 的快捷方式,专门处理 HTTP PUT 请求,通常用于更新资源。
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CustomerController {
@PutMapping("/customers")
public String updateCustomer(@RequestBody Customer customer) {
return "Customer updated successfully";
}
}
@DeleteMapping
作用:是 @RequestMapping(method = RequestMethod.DELETE) 的快捷方式,专门处理 HTTP DELETE 请求,通常用于删除资源。
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EmployeeController {
@DeleteMapping("/employees/{id}")
public String deleteEmployee(@PathVariable Long id) {
return "Employee deleted successfully";
}
}
1.3. 请求参数注解
@RequestParam
作用:用于从请求的 URL 参数中获取值,并绑定到方法的参数上。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SearchController {
@GetMapping("/search")
public String search(@RequestParam String keyword) {
return "Search results for: " + keyword;
}
}
@PathVariable
作用:用于从请求的 URL 路径中获取变量值,并绑定到方法的参数上。
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ArticleController {
@GetMapping("/articles/{id}")
public String getArticle(@PathVariable Long id) {
return "Article with ID: " + id;
}
}
@RequestBody
作用:用于将 HTTP 请求体中的数据绑定到方法的参数上,通常用于处理 JSON 或 XML 格式的数据。使用 @RequestBody 接收 JSON 数据时,Spring MVC 会自动将 JSON 数据反序列化为对应的 Java 对象。
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MessageController {
@PostMapping("/messages")
public String sendMessage(@RequestBody Message message) {
return "Message sent: " + message.getContent();
}
}
1.4. 其他注解
@ModelAttribute
作用:可以将请求参数绑定到一个 Java 对象上,也可以用于在控制器方法调用前将数据添加到模型中。
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FormController {
@GetMapping("/form")
public String showForm(@ModelAttribute("user") User user, Model model) {
return "form";
}
}
@SessionAttributes
作用:用于将模型中的属性存储到 HTTP 会话中,以便在多个请求之间共享数据。
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
@Controller
@SessionAttributes("user")
public class SessionController {
@GetMapping("/setUser")
public String setUser(Model model) {
model.addAttribute("user", new User("John", 25));
return "redirect:/getUser";
}
@GetMapping("/getUser")
public String getUser(Model model) {
User user = (User) model.getAttribute("user");
return "User: " + user.getName();
}
}