1.@RestController
@RestController = @Controller + @ResponseBody
@ResponseBody主要用于返回一个Json字符串,可以将其他类型转换成Json类型来输出。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
* @since 4.0.1
*/
@AliasFor(annotation = Controller.class)
String value() default "";
}
2.@SpringBootApplication
@SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan
在启动类中添加@SpringBootApplication或者添加这三个注解都是可以的。
@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) })
public @interface SpringBootApplication {
...
}
@EnableAutoConfiguration:启用Spring Boot的自动配置;
@ComponentScan:在应用程序所在的程序报上启用扫描;
@SpringBootConfiguration:允许在上下文中注册额外的bean或导入其他配置类。