@RestController

@RequestMapping("/hello")

public class HelloController {

    @GetMapping("/hello")

    public String hello() {

        System.out.println("hello springboot!!!");

        return "hello springboot";

    }

}

[](

)4、编写入口类 Application


// @EnableAutoConfiguration // 作用: 开启自动配置 初始化spring环境 springmvc环境

// @ComponentScan // 作用: 用来扫描相关注解 扫描范围 当前入口类所在的包及子包(com.yusal及其子包)

// 下面的注解的功能是上面两个注解功能之和

@SpringBootApplication

public class Application {

    public static void main(String[] args) {

        // springApplication: spring应用类    作用: 用来启动springboot应用

        // 参数1: 传入入口类 类对象   参数2: main函数的参数

        SpringApplication.run(Application.class, args);

    }

}

[](

)5、启动项目并访问


运行 Application 中的 main 来启动整个项目:如下则启动成功。

.   ____          _            __ _ _

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )

  '  |____| .__|_| |_|_| |_\__, | / / / /

 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::        (v2.2.5.RELEASE)



2020-06-19 21:56:45.166  INFO 8888 --- [           main] com.yusael.Application                   : Starting Application on DESKTOP-PNEMUCC with PID 8888 (D:\CodePro\IdeaPro\SpringBoot\hello_springboot\target\classes started by yusael in D:\CodePro\IdeaPro\SpringBoot)

2020-06-19 21:56:45.171  INFO 8888 --- [           main] com.yusael.Application                   : No active profile set, falling back to default profiles: default

2020-06-19 21:56:46.716  INFO 8888 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8989 (http)

2020-06-19 21:56:46.730  INFO 8888 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]

2020-06-19 21:56:46.731  INFO 8888 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.31]

2020-06-19 21:56:46.855  INFO 8888 --- [           main] o.a.c.c.C.[.[.[/hello_springboot]        : Initializing Spring embedded WebApplicationContext

2020-06-19 21:56:46.856  INFO 8888 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1613 ms

2020-06-19 21:56:47.244  INFO 8888 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'

2020-06-19 21:56:47.473  INFO 8888 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8989 (http) with context path '/hello_springboot'

2020-06-19 21:56:47.487  INFO 8888 --- [           main] com.yusael.Application                   : Started Application in 3.253 seconds (JVM running for 4.806)

2020-06-19 21:57:28.156  INFO 8888 --- [nio-8989-exec-3] o.a.c.c.C.[.[.[/hello_springboot]        : Initializing Spring DispatcherServlet 'dispatcherServlet'

2020-06-19 21:57:28.157  INFO 8888 --- [nio-8989-exec-3] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'

2020-06-19 21:57:28.165  INFO 8888 --- [nio-8989-exec-3] o.s.web.servlet.DispatcherServlet        : Completed initialization in 8 ms

注意:springboot 的项目默认没有项目名,项目名要在 application.yml 中配置;

如果不在配置文件中配置,路径应该是:

http://localhost:8080/hello/hello

由于我们在配置文件中进行了配置:

server:

  port: 8989 #用来指定内嵌服务器端口号

  context-path: /springboot  #用来指定项目的访问路径

所以访问路径变成了:

http://localhost:8989/springboot/hello/hello

[](

)相关注解


[](

)@EnableAutoConfiguration

  • 作用:开启自动配置 ,根据 pom.xml 文件中依赖自动判断并构建环境
    如在第一个环境之中引入了 spring-boot-starter-web, 会自动根据引入的这个依赖构建相关环境(springmvc环境、web容器环境)
  • 修饰范围:只能用在类上

[](

)@ComponentScan

  • 作用:用来开启注解扫描
  • 修饰范围:只能用在类上
  • 扫描注解范围:默认当前包以及当前包下的子包

@SpringBootApplication@EnableAutoConfiguration + @ComponentScan

[](

)@RestController

  • 作用:用来实例化当前对象为一个控制器对象,并将类上的所有方法的返回值转为 json,响应给浏览器。
  • 修饰范围: 用在类上
  • 功能相当于:@Controller + @ResponseBody
  • @Controller:实例化当前类为一个控制器
  • @ResponseBody:用来将当前方法返回值转为 json, 相应给浏览器

[](

)@RequestMapping

  • 作用:用来加入访问路径
  • 修饰范围:类(加入命名空间)、方法上(指定具体路径)
  • 同类注解:
    @GetMapping:作用: 限定请求方式只能是GET,并指定路径;修饰范围:方法上
    @PostMapping@DeleteMapping@PutMapping