SpringBoot常用注解

@SpringBootApplication

组合注解,SpringBoot应用程序的入口类。用于启动程序。

复制代码
@SpringBootApplication
public class CloudElasticSearchApplication {
    public static void main(String[] args) {
        SpringApplication.run(CloudElasticSearchApplication.class, args);
    }
}

内部包括@Configuration、@EnableAutoConfiguration和@ComponentScan三个注解。

复制代码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)

@Configuration用于创建和配置Spring应用程序上下文中的Bean。

@EnableAutoConfiguration用于自动添加所需的依赖项和配置。

@ComponentScan用于扫描当前包及其子包中的所有类,如果某些类使用了@Component、@Controller等注解,注入Spring容器中。

@RestController

@RestController处理请求时,会自动将方法的返回值转换为JSON格式的响应体,并返回给客户端。其包含了@Controller和@ResponseBody功能,如下所示:

复制代码
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
    @AliasFor(
        annotation = Controller.class
    )
    String value() default "";
}

@Controller将该类声明进入Spring容器中进行管理

@ResponseBody表明该类的所有方法的返回值都直接进行提交而不经过视图解析器,且返回值的数据自动封装为json的数据格式

@GetMapping、@PostMapping、@PutMapping、@DeleteMapping

@RequestMapping用来处理请求地址映射的注解,可以用在类或方法上。@GetMapping则是在其基础上指定方法为GET(method=RequestMethod.GET),用于映射HTTP GET请求。同理@PostMapping、@PutMapping、@DeleteMapping也是如此。例如@GetMapping的内部实现如下:

复制代码
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(
    method = {RequestMethod.GET}
)
public @interface GetMapping {......}

具体使用:

复制代码
@RestController
@RequestMapping("/home")
public class ElasticSearchClientConfig {
    @GetMapping("/user")
    public String test(){
        return "nih";
    }
}

@RequestParam

用于获取请求参数的值。可以通过@RequestParam("id")或者@RequestParam(value = "id")指定传入的参数名。

复制代码
@RestController
@RequestMapping("/home")
public class ElasticSearchClientConfig {
    @GetMapping("/user")
    public String test(@RequestParam(value = "name")String name ){
        return "nih";
    }
}

@Slf4j

Lombok框架中的注解,用来生成日志记录器。

复制代码
@Slf4j
public class ElasticSearchClientConfig {
    public String test(){
        log.debug("1");
        log.info("2");
        log.error("3");
        return "nih";
    }
}

@Data、@NoArgsConstructor、@AllArgsConstructor

@Data用于自动生成实体类中get()、set()、toString()、hashCode()和equals()方法。@NoArgsConstructor提供类的无参构造方法。@AllArgsConstructor提供类的全参构造方法。

复制代码
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
	private String name;
    private String password;
}
相关推荐
lqbzlll8 分钟前
Spring Boot 开启虚拟线程后接口反而变慢了?4 个排查方向和对应修法
java·spring boot·后端
七夜zippoe13 分钟前
LangChain4j vs Spring AI:Java AI 框架怎么选(对比表)
java·人工智能·spring·ai·langchain4j
悠仁さん18 分钟前
【C++】set map 使用
java·前端·c++
yuniko-n44 分钟前
【JUC】Thread 内存泄露
java
freejackman1 小时前
【架构篇】双遍历 + 多栈协同:手撸 SQL 血缘引擎的架构思考
java·大数据·数据库·hive·sql·架构·spark
企业数字化笔记1 小时前
业务单据怎样接入流程中心?启动条件、实例关联与失败回退
java·spring boot·mysql
挖掘狂人1 小时前
同一个端口能不能被两个程序同时监听?
java·后端·程序员
codigger1 小时前
同一个端口能不能被两个程序同时监听?
java·后端·程序员·编程
西索斯coding1 小时前
qwen3.8-max-0902 和 minimax-m3 改真实仓库差多少?2026 年 50 个缺陷复现实测框架
java·服务器·网络·ai
孤狼GPT1 小时前
ChatGPT、Codex工程实战:Spring Boot的SSE一直在推送,为什么经过Nginx后前端才一次性显示?
spring boot·chatgpt·sse·codex·chatgptplus