写在前面
又忘记怎么搭建1个SpringBoot项目了,还是要记录下啊!
我知道可以去官网直接下载项目包,也知道idea可以直接新建1个springboot项目,但是这也屏蔽了我对原始的思考。
这次就对着官网来吧,也不去看任何博客了。
准备用1个最简单的maven项目一步步搭建出springboot项目,记住配置文件的操作。
版本:
window10 + idea+maven3.5
公司用的2.1.14.RELEASE,我也准备用。
步骤
1、参照官网
https://docs.spring.io/spring-boot/docs/2.2.13.RELEASE/reference/html/getting-started.html#getting-started
参考官网用maven引入依赖测试接口的例子,我也是这么做的。
2、pom
新建1个maven项目,引入这个依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.14.RELEASE</version>
</parent>
3、写一个启动类
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class Run {
/*本地测试这个接口只有稳定3,4ms*/
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Run.class, args);
}
}
官网的写法就是这样,很优秀,直接1个controller作为启动类。
里面的源码包括注解有机会还是要啃一啃的。
以上就可以本地运行了,要想打成jar包,还得引入springboot自己搞的1个maven插件!
这个坑也是踩了好久,官网也没说。
<build>
<!-- 打包springboot项目所用插件。否则mvn package后jar包执行会报错:springbootdemo-1.0-SNAPSHOT.jar中没有主清单属性-->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
之后打包成功,本地cmd可以运行这jar包,测试成功的!