1、创建一个maven工程
填写项目相关信息之后等待项目引入相关依赖
2、在pom.xml中加入以下依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> </parent>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
|
3、在main下新建com.syl.MyFirstSpringBootApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.syl;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class MySpringBootApplication {
public static void main(String[] args) { SpringApplication.run(MySpringBootApplication.class, args); } }
|
4、在main下新建com.syl.controller.MyController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.syl.controller;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;
@Controller public class MyColtroller {
@ResponseBody @RequestMapping("/hello") public String show(){ return "hello world"; } }
|
注意:MyFirstSpringBootApplication.java必须要在MyController.java的同包或者父包中,不然会报404错误
5、测试
浏览器输入http://localhost:8080/hello 回显helloworld表示入门程序创建成功
6、IDEA配置springBoot热部署
添加依赖
1 2 3 4 5
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency>
|
右上角配置springBootApplication
Running Application Update Policies
On “Update’ action: Update classes and resources
On frame deactivation: Update classes and resources
重启项目即可
7、将项目打包成可执行jar
引入插件
1 2 3 4 5 6 7 8
| <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
|
执行maven的package命令并等待执行完成
执行完成之后在项目目录下找到
target\firstspringboot-1.0-SNAPSHOT.jar
在此处打开命令行并执行
1
| java -jar firstspringboot-1.0-SNAPSHOT.jar
|
在浏览器中输入http://localhost:8080/hello,一样可以访问该项目