【SpringBoot】开发常见注解

目录

    • Controller
      • @Controller
      • @RestController
      • @ResponseBody
      • @GetMapping 与 @PostMapping
      • @RequestMapping
      • @RequestParam
      • @PathVariable
      • @RequestBody
      • @Valid
    • Service
      • @Service
      • @Autowired
      • @Resource
      • @Scheduled
      • @Async
    • Entity
      • @Data
      • @JsonFormat
      • @ToString
      • @TableName("xxx")
      • @TableField
      • @ApiModel("xxx对象")
      • @Accessors(chain = true)
    • DAO
      • @Repository
      • @Mapper
    • 配置
      • @Configuration
      • @ComponentScan
      • @Component
      • @Bean
      • @PostConstruct
      • @Conditional
      • @ConfigurationProperties
    • 其他
      • @Slf4j
      • @Value

Controller

@Controller

标识一个Spring MVC控制器,用于处理HTTP请求。

@RestController

@RestController 注解是 @Controller 和 @ResponseBody 两个注解的组合。@Controller 注解表明这个类是一个 Spring MVC 控制器,而 @ResponseBody 注解则表明控制器的方法返回值应该绑定到 web 响应体上。

@RestController 是 Spring Boot 中的一个非常核心的注解,它用于标注在类上,表明这个类是一个控制器(Controller),并且所有的方法返回值都会直接作为 HTTP 响应体返回给客户端,这通常用于构建 RESTful Web 服务。

@ResponseBody

@ResponseBody 是 Spring MVC 和 Spring Boot 中用于标注在控制器(Controller)的方法上的注解。它表明该方法的返回值应该直接作为 HTTP 响应体返回给客户端,而不是被解析为跳转路径或模板名称。

@Controller  
public class MyController {
     
  
    @GetMapping("/hello")  
    @ResponseBody  
    public String hello() {
     
        return "Hello, Spring MVC!";  
    }  
}

@GetMapping 与 @PostMapping

@GetMapping @PostMapping 是用于处理 HTTP GET 和 POST 请求的注解。它们分别对应于 HTTP 协议中的 GET 和 POST 方法,常用于构建 RESTful Web 服务。

@RestController  
public class MyController {
     
  
    @GetMapping("/hello")  
    public String hello() {
     
        return "Hello, GET request!";  
    }
    @PostMapping("/submit")  
    public String submit(@RequestParam String data) {
     
        // 处理提交的数据  
        return "Received data: " + data;  
    }  
}

支持配置多个路径:

@GetMapping(value = {"/v1/hello", "/v2/hello"})

@RequestMapping

@RequestMapping用于处理 HTTP 请求的注解。它可以标注在方法上,用于将 HTTP 请求映射到特定的处理方法上。

@Controller
@RequestMapping("/users")  
public class UserController {
     
  
    @RequestMapping(value = "/{userId}", method = RequestMethod.GET)  
    public String getUser(@PathVariable("userId") String userId, Model model) {
     
        // 处理 GET 请求,路径如 /users/{userId}  
        return "user";  
    }  
  
    @RequestMapping(value = "/new", method = RequestMethod.GET)  
    public String newUserForm(Model model) {
     
        // 处理 GET 请求,路径如 /users/new  
        return "userForm";  
    }  
}

@RequestParam

@RequestParam将HTTP请求中的查询参数或表单参数绑定到控制器方法的参数上。
如果你有一个请求URL为/user?name=John,那么使用@RequestParam(“name”) String userName可以将查询参数name的值John绑定到方法参数userName上。

@PathVariable

@PathVariable将URL模板变量值绑定到控制器方法的参数上。这通常用于RESTful风格的URL,其中URL的一部分表示资源的标识符。
如果你有一个请求URL为/users/123,并且控制器方法定义为@GetMapping(“/users/{userId}”),那么使用@PathVariable(“userId”) String userId可以将URL模板变量userId的值123绑定到方法参数userId上。

@RequestBody

@RequestBody将HTTP请求体的内容绑定到控制器方法的参数上。这通常用于处理客户端发送的JSON或XML数据。
如果你有一个请求体为{“name”:“John”, “age”:30}的POST请求,并且控制器方法定义为@PostMapping(“/user”),那么使用@RequestBody User user可以将请求体中的JSON数据绑定到方法参数user上,其中User是一个具有name和age属性的Java类。

@RestController  
@RequestMapping("/api")  
public class MyController {
     
  
    // 使用@RequestParam绑定查询参数  
    @GetMapping("/greeting")  
    public ResponseEntity<String> greeting(@RequestParam(name = "name", defaultValue = "World") String name) {
     
        return ResponseEntity.ok("Hello, " + name + "!");  
    }  
  
    // 使用@PathVariable绑定URL模板变量  
    @GetMapping("/users/{userId}")  
    public ResponseEntity<String> getUser(@PathVariable("userId") String userId) {
     
        return ResponseEntity.ok("User ID: " + userId);  
    }  
  
    // 使用@RequestBody绑定请求体内容  
    @PostMapping("/user")  
    public ResponseEntity<String> createUser(@RequestBody User user) {
     
        // 假设User是一个具有name和age属性的Java类  
        return ResponseEntity.ok("User created: " + user.getName() + ", " + user.getAge());  
    }  
}

@Valid

@Valid注解在 Spring 框架中用于开启对方法参数或返回值的自动验证,通常与 @NotNull@Size等一起使用以验证 Java Bean 的属性。

@RestController  
@RequestMapping("/api")  
public class ValidationController {
     
  
    @PostMapping("/create")  
    public ResponseEntity<String> createItem(@Valid @RequestBody Item item) {
     
        // 如果验证通过,则处理数据  
        return ResponseEntity.ok("Item created: " + item.getName());  
    }  
  
    public static class Item {
     
        @NotBlank(message = "Name is required and cannot be blank")  
        private String name;  
        @Min(1)  
        private int quantity;  
        // 省略构造函数、getter和setter方法  
    }  
}

Service

@Service

@Service 注解主要用于标记业务逻辑层的服务组件。这个注解是 @Component 的一个特化版本,用于指示一个类应该被视为Spring应用上下文中的一个Bean。当Spring进行组件扫描时,它会自动检测并实例化所有带有 @Service 注解的类,并将它们注入到依赖于这些服务的其他Bean中。

@Autowired

@Autowired 用于实现依赖注入,可以用在变量、构造方法、setter 方法上。Spring 在进行依赖注入时,会先尝试按类型(byType)进行匹配,如果找到多个相同类型的 Bean,则会按名称(byName)进行匹配。

@Service  
public class UserService {
      
    

你可能感兴趣的:(Spring,Boot,Java,Spring,spring,boot,后端,java)