7月
07
创建spring boot项目
- 选择maven项目

- 输入项目信息

GroupId:一般是公司域名 Arttifactid:项目名称 Version:版本号,SNAPSHOT表示目前是快照版本,快照版本提交中央仓库不需要审核,并且能够实时更新
- 配置依赖
添加以下内容到pom.xml
文件中
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.7.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
- 创建启动类
package com.definesys.train; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @Description: * @author: jianfeng.zheng * @since: 2022/7/7 3:46 下午 * @history: 1.2022/7/7 created by jianfeng.zheng */ @SpringBootApplication() public class MainApp { public static void main(String[] cmd) { SpringApplication.run(MainApp.class); } }
- 创建配置文件
在resources
文件夹下创建文件appliction.properties
,这个是spring boot的配置文件,比如下面这个配置就是更改启动端口号(默认是8080)
server.port=8081
- 启动
在启动类MainApp
上右健Run
启动,或者Debug
进入调试
创建RestController
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * @Description: * @author: jianfeng.zheng * @since: 2022/7/7 3:57 下午 * @history: 1.2022/7/7 created by jianfeng.zheng */ @RestController @RequestMapping("employee") public class EmployeeController { @GetMapping("employeeName") public String getEmployeeName(@RequestParam("userId") String userId) { return "jianfeng"; } }
- 必须使用
RestController
注解 - 一个controller一般对应一个功能模块,建议在controller上面添加注解
RequestMapping
指定controller访问路径
Address: https://zhengjianfeng.cn/?p=653
no comment untill now