springboot的controller层的常用注解说明

 更新时间:2023年10月25日 16:32:34   作者:旺仔001  
这篇文章主要介绍了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
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • SpringBoot结合Redis实现接口幂等性的示例代码

    SpringBoot结合Redis实现接口幂等性的示例代码

    本文主要介绍了SpringBoot结合Redis实现接口幂等性的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • Mybatis批量操作sql写法示例(批量新增、更新)

    Mybatis批量操作sql写法示例(批量新增、更新)

    Mybatis技术,现在是工作中使用频率越来越高,我们在对数据库进行操作的时候,经常会遇到批量操作的需求,这篇文章主要给大家介绍了关于Mybatis批量操作sql写法的相关资料,需要的朋友可以参考下
    2021-05-05
  • spring-boot使用AOP统一处理日志

    spring-boot使用AOP统一处理日志

    这篇文章主要为大家详细介绍了spring-boot使用AOP统一处理日志,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12
  • 使用idea的database模块绘制数据库er图的方法

    使用idea的database模块绘制数据库er图的方法

    这篇文章主要介绍了使用idea的database模块绘制数据库er图,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-07-07
  • 详解Spring MVC事务配置

    详解Spring MVC事务配置

    这篇文章主要介绍了详解Spring MVC事务配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • Java 给PPT添加动画效果的示例

    Java 给PPT添加动画效果的示例

    这篇文章主要介绍了Java 给PPT添加动画效果的示例,帮助大家更好的理解和学习使用Java,感兴趣的朋友可以了解下
    2021-04-04
  • OpenCV Java实现人脸识别和裁剪功能

    OpenCV Java实现人脸识别和裁剪功能

    这篇文章主要为大家详细介绍了OpenCV Java实现人脸识别和裁剪功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-05-05
  • 解决IDEA启动弹窗的问题

    解决IDEA启动弹窗的问题

    文章描述了如何解决JetBrains IDEA启动时弹出的“JetbrainsAgentloadssuccessfully!”提示弹窗的问题,解决方法是将解压后的“important.txt”文件复制到IDEA的bin目录下,确保与“jetbrains-agent.jar”文件在同一目录
    2026-02-02
  • java运行时数据区域和类结构详解

    java运行时数据区域和类结构详解

    这篇文章主要介绍了java运行时数据区域和类结构,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • SpringBoot集成PostgreSQL并设置最大连接数

    SpringBoot集成PostgreSQL并设置最大连接数

    本文主要介绍了SpringBoot集成PostgreSQL并设置最大连接数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-11-11

最新评论