springboot的controller层的常用注解

在Spring Boot中,Controller层是用来处理HTTP请求的组件。下面是Controller层中常用的注解:

1、@RestController

将一个类标识为控制器,并使其支持RESTful风格的API。它是@Controller和@ResponseBody的组合注解。

@Controller 将当前修饰的类注入SpringBoot IOC容器,使得从该类所在的项目跑起来的过程中,这个类就被实例化。
@ResponseBody 它的作用简短截说就是指该类中所有的API接口返回的数据,甭管你对应的方法返回Map或是其他Object,它会以Json字符串的形式返回给客户端

@RestController
public class UserController {
    
    
    // Controller methods
}

2、@RequestMapping

映射HTTP请求到处理方法或控制器类级别。可以用于类级别的注解来定义基本的URL路径,并且可以在方法级别的注解中添加进一步的路径。

@RestController
@RequestMapping("/users")
public class UserController {
    
    
    // Methods with specific request mappings
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
    
    
        // Method implementation
    }

    @PostMapping
    public User createUser(@RequestBody User user) {
    
    
        // Method implementation
    }
}

3、@GetMapping, @PostMapping, @PutMapping, @DeleteMapping, @PatchMapping

分别映射HTTP的GET、POST、PUT、DELETE和PATCH请求到处理方法。

@RestController
@RequestMapping("/users")
public class UserController {
    
    
    @GetMapping("/{id}")
    public User getUserById(@PathVariable Long id) {
    
    
        // Method implementation
    }

    @PostMapping
    public User createUser(@RequestBody User user) {
    
    
        // Method implementation
    }

    @PutMapping("/{id}")
    public User updateUser(@PathVariable Long id, @RequestBody User user) {
    
    
        // Method implementation
    }

    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Long id) {
    
    
        // Method implementation
    }

    @PatchMapping("/{id}")
    public User partialUpdateUser(@PathVariable Long id, @RequestBody UserPartialUpdateRequest request) {
    
    
        // Method implementation
    }
}

4、@PathVariable

用于将URL路径中的占位符参数绑定到处理方法的参数上

@GetMapping("/users/{id}")
public User getUserById(@PathVariable Long id) {
    
    
    // Method implementation
}

5、@RequestParam

用于将请求参数绑定到处理方法的参数上。可以指定参数的名称、是否必需以及默认值。

@GetMapping("/users")
public List<User> getUsersByRole(@RequestParam("role") String role) {
    
    
    // Method implementation
}

6、@RequestBody

用于将请求体中的数据绑定到处理方法的参数上,通常用于处理POST请求的JSON数据。

@PostMapping("/users")
public User createUser(@RequestBody User user) {
    
    
   // Method implementation
}

7、@RequestHeader

用于将请求头中的信息绑定到处理方法的参数上。

@GetMapping("/users")
public List<User> getUsersByLocale(@RequestHeader("Accept-Language") String locale) {
    
    
    // Method implementation
}

8、@ResponseBody

将方法的返回值直接作为HTTP响应的内容返回,而不是将其解析为视图。

@GetMapping("/users/{id}")
@ResponseBody
public User getUserById(@PathVariable Long id) {
    
    
    // Method implementation
}

9、@ResponseStatus

设置响应的HTTP状态码。

@DeleteMapping("/users/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteUser(@PathVariable Long id) {
    
    
   // Method implementation
}

10、@ModelAttribute

用于绑定请求参数到一个模型对象,并使其可在视图中访问。

@GetMapping("/users/{id}")
public String getUserDetails(@PathVariable Long id, @ModelAttribute("message") String message) {
    
    
    // Method implementation
}

11、@Valid

用于验证绑定的请求参数,结合JSR-303 Bean Validation规范进行数据校验。

@PostMapping("/users")
public ResponseEntity<?> createUser(@Valid @RequestBody User user, BindingResult result) {
    
    
    if (result.hasErrors()) {
    
    
        // Handle validation errors
    }
    // Method implementation
}

猜你喜欢

转载自blog.csdn.net/weixin_44727769/article/details/131001174