在 maven目录/conf/setting.xml 中添加镜像坐标
<mirror>
<id>nexus-aliyunid>
<mirrorOf>centralmirrorOf>
<name>Nexus aliyunname>
<url>http://maven.aliyun.com/nexus/content/groups/publicurl>
mirror>

将 start.spring.io 改为 start.aliyun.com

CTRL 点进去可以看到 :


通过这里的 properties 和 dependencyManagement 统一进行版本管理


通过阿里云创建的项目会有一点不同
没有继承,而是在 pom.xml 文件中直接引用

主要是跟 Springboot 的版本有关
SpringBoot 程序要继承 spring-boot-starter-parentspring-boot-starter-parent 中定义了若干个依赖管理parent 模块可以避免多个依赖使用相同技术时出现依赖版本冲突parent 的形式也可以采用引入依赖的形式实现效果继承只有一次,用过之后就没有机会了

在每个 starter 依赖中, 导入了需要使用的其他依赖
不需要再手动导入相关坐标, 减少了依赖配置
starter 本质上是依赖
parent 本质上是依赖管理, 管理依赖的版本, 定义了版本坐标
在开发中导入坐标不需要写依赖版本, 只需要写 groupId 和 artifactId
如果发生版本错误再手动指定版本
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
以上为引导类
这里运行 run 方法, 就是初始化了一个 Sping 容器
SpringBoot 的引导类时 Boot 工程的执行入口, 运行 main 方法就可以启动项目
SpringBoot 工程运行后初始化 Spring 容器, 扫描引导类所在包加载 Bean
在 spring-boot-starter-web 中, 存在以下依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
<version>2.6.3version>
<scope>compilescope>
dependency>
这个依赖中, 存在内嵌的 tomcat

REST (Representational State Transfer), 表现形式状态转换
上述行为是约定方式, 约定不是规范, 可以打破, 所以称REST风格, 而不是REST规范
描述模块的名称通常使用复数, 也就是加 s 的格式描述, 表示此类资源, 而非单个资源, 例如: users, books, accounts…
根据 REST 风格对资源进行访问称为 RESTful
@RequestMapping@RequestMapping(value = "/users", method = RequestMethod.POST)
@RespondBody
public String save(@RequestBody User user){
System.out.println("user save..." + user);
return "{'module':'user save'}";
}
value (默认): 请求访问路径method : http 请求动作: 标准动作(GET/POST/PUT/DELETE)@PathVariable@RequestMapping(value = "/users/{id}", method = RequestMethod.DELETE)
@RespondBody
public String delete(@PathVariable Integer id){
System.out.println("user delete..." + id);
return "user delete";
}
@RequestParam 用于接收 url 地址传参或表单传参@RequestBody 用于接收 json 数据@PathVariable 用于接收路径参数, 使用 {参数名称} 描述路径参数应用@RequestBody 应用较广@RequestParam 接收请求参数@PathVariable 接收请求路径变量, 通常用于传递 id 值模板引擎
使用 thymeleaf, 只需要导入对应的依赖
将 html 文件放在 templates 目录下
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
在html文件中导入命名空间
<html lang="en" xmlns: th="http://www.thymeleaf.org">
基础表达式
Variable Expressions: ${...}
Selection Variable Expressions: *{...}
Message Expressions: #{...}
Link URL Expressions: @{...}
Fragment Expressions: ~{...}