1.工具说明:
使用idea+maven方式。项目的层次结构
2.新建springboot工程
3.在pom.xml中导入springboot入门程序所需的依赖坐标
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
4.编写springboot主程序代码
package com.example.MainApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//标志主程序类,表明这是一个springboot应用
@SpringBootApplication
public class SpringBootMain {
public static void main(String[] args) {
//加载springboot主程序
SpringApplication.run(SpringBootMain.class,args);
}
}
5.编写controller层代码
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class helloController {
@ResponseBody
@RequestMapping("/hello")
public String sayHello(){
return "hello SpringBoot";
}
}
6.回到第4步写的主程序,运行主程序
7.观察控制台,我们可以看到Tomcat已经启动了
8.在浏览器发送hello请求,访问helloController中的sayHello方法
http://localhost:8080/hello
8.总结
这个入门程序就相当于使用了springmvc的功能,处理浏览器发送的请求。在controller层中的@ResponseBody和@RequestMapping两个注解就是springmvc的注解。
但跟使用springmvc实现处理hello请求不同的是,使用springboot不需要我们书写springmvc的配置文件,也没用导入springmvc的依赖(其实是被springboot的依赖封装起来的,可以点进去springboot的依赖坐标看的,方法就是按住ctrl键点击pom文件中的依赖)
简介,减少配置文件,这就是springboot的强大之处。