3. 自定义控制器
小于 1 分钟
我们创建的是一个基于SpringBoot的WEB项目,那么怎么处理客户端提交的请求呢?这时我们可以直接在启动器所在的子目录下创建对应的Controller即可
package com.bobo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/*@Controller
@ResponseBody*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
System.out.println("hello ...");
return "Hello ...";
}
}
访问即可

为什么能够扫描到这个@Controller注解,根本原因是在我们的启动器中的那个@SpringBootApplication注解,这个注解本身是一个组合注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
// 以上四个是JAVA中提供的元注解
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
注解 | 说明 |
---|---|
@SpringBootConfiguration | 也是一个组合注解,包含@Configuriation,标注当前注解的类是一个配置类 |
@EnableAutoConfiguration | 完成依赖的自动配置的一个注解,是自动装配的核心注解 |
@ComponentScan | 指定扫描路径的注解,如果不去指定默认会将当前类所在的目录及其子目录作为扫描路径 |