- JDK 1.8 或 更高版本;
- maven3.x:maven 3.3以上版本;
- IntelliJ IDEA、STS;
- Spring Boot 正式版为 2.6.9;
设置maven编译器为1.8版本,给maven 的settings.xml配置文件的profiles标签添加:
2.3 项目实现功能jdk-1.8 true 1.8 1.8 1.8 1.8
浏览器发送hello请求,服务器接受请求并处理,响应Hello World字符串。
主要通过两种形式来创建spring boot项目:
- 创建maven项目,添加相应的pom依赖。
- 使用 spring boot 提供的 Initializer 来创建项目。
2.3.3 编写主程序4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.9 com.newcapec springboot01-helloworld 1.0-SNAPSHOT jar org.springframework.boot spring-boot-starter-web
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
//通过SpringApplication启动程序
SpringApplication.run(MainApplication.class, args);
}
}
2.3.4 编写Controller
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@GetMapping("/hello")
public String hello(){
System.out.println("hello方法执行了...");
return "hello springboot我来了...";
}
}
2.3.5 运行测试
回到MainApplicaiton 类中,运行main()方法,启动springboot应用程序:
在浏览器中输入localhost:8080来测试:
在浏览器中输入localhost:8080/hello来测试:
2.3.6 简化部署Springboot打包插件,可以将应用打包成一个可执行的jar包。
org.springframework.boot spring-boot-maven-plugin
调用idea 的maven插件,执行package来执行打包命令
进入 cmd 命令行窗口,定位到打包后打jar文件所在目录。
将这个应用打成jar包,直接使用 java -jar springboot01-helloworld-1.0-SNAPSHOT.jar 的命令进行执行。
程序执行成功后,可再次通过浏览器来验证hello服务是否正确返回。
2.4 Spring Initializr构建一个Sping Boot的Maven项目,强烈推荐Spring Initializr(https://start.spring.io),它从本质上来说就是一个Web应用程序,它能为你生成Spring Boot项目结构。
通过IntelliJ IDEA使用,创建Spring Boot操作步骤如下:
- 在File菜单里面选择 New > Project;
- 然后选择Spring Initializr;
- 填写项目信息;
- 选项Springboot版本和依赖。
默认生成的Spring Boot项目:
- 主程序已经生成好了,只需要添加自己所需的业务逻辑控制代码(controller,service,dao等)。
- src,源码目录。
- resources文件夹中目录结构。
- static:存放所有的静态资源(js,css,images);
- templates:保存所有的模板页面。Spring Boot默认jar包使用嵌入式的Tomcat,不支持JSP页面,可以使用模板引擎(freemarker、thymeleaf);
- application.properties:Spring Boot应用的配置文件。
- .gitignore–分布式版本控制系统git的配置文件,意为忽略提交。
- mvnw–全名是maven wrapper,它的原理是在maven-wrapper.properties文件中记录你要使用的maven版本,当用户执行mvnw clean 命令时,发现当前用户的maven版本和期望的版本不一致,那么就下载期望的版本,然后用期望的版本来执行mvn命令,比如刚才的mvn clean。
- .mvn文件夹–存放maven-wrapper.properties和相关jar包。
- mvn.cmd–执行mvnw命令的cmd入口。
- .Idea存放项目的配置信息,包括历史记录,版本控制信息等。其实可以不用理它,也可以在IDEA里面设置隐藏它。



