1. 配置环境
jdk1.8及以上 (配置好了之后可以在命令提示符中查看一下:java -version 以便后面打成jar包测试)
idea (我目前使用的是2018的)
2. 开始搭建
1,打开idea 开始创建maven项目
2. 创建好之后是这样的(刚创建好的traget文件是没有的)
- 在pom.xml 文件中添加
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--下面替换成自己创建时的路劲和名字-->
<groupId>com.ye</groupId>
<artifactId>springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/>
</parent>
<dependencies>
<!--SpringBoot启动依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--这是生成get set 方法的 目前不需要-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<!--maven项目打包 打包的时候会用到-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
-
包的架构图如下:
-
在com.ye路径下创建一个启动类DemoApplication
package com.ye; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
-
在controller包下创建一个TestController
package com.ye.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("test") public class TestController { @GetMapping("/1") public String test(){ return "Hello World"; } }
-
最好在resources 包下新建一个application.yml文件
server: port: 8081 # 不写的话,默认端口是8080 防止被其他应用占用
以上就是springboot的搭建了