SpringBoot高级

1. 整合MyBatis-Plus 

1.1 基本配置

MyBatis-Plus (opens new window) (简称 MP )是一个 MyBatis (opens new window) 的增强工具,在MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

1.2 代码生成器()

依赖 mybatis plus 部分依赖
<!-- mp -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!-- 代码生成工具 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!-- 代码生成模板 -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
代码生成器 ( )
package com.xja;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.Scanner;
public class CodeGenerator {
//数据源
private static final String JdbcUrl = "jdbc:mysql:///boot?
useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8";
private static final String DriverName = "com.mysql.cj.jdbc.Driver";
private static final String username = "root";
private static final String password = "";
//忽略的表前缀
private static final String tablePrefix = "tb_";
//private static final String tablePrefix = "";
//父包名
private static final String parent = "com.xja";
private static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入" + tip + ":");
if (scanner.hasNext()) {
String ipt = scanner.next();
if (ipt!=null) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
// 创建代码生成器对象
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(scanner("你的项目路径(本项目的路径
如:D:\\Java_practice\\demo)") + "/src/main/java");
gc.setAuthor("sunwz");
//生成之后是否打开资源管理器
gc.setOpen(false);
//重新生成时是否覆盖文件
gc.setFileOverride(false);
//%s 为占位符
//mp生成service层代码,默认接口名称第一个字母是有I
gc.setServiceName("%sService");
//gc.setServiceName("service");
//设置主键生成策略 自动增长
gc.setIdType(IdType.AUTO);
//设置Date的类型 只使用 java.util.date 代替
gc.setDateType(DateType.ONLY_DATE);
//开启实体属性 Swagger2 注解
//gc.setSwagger2(true);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(JdbcUrl);
dsc.setDriverName(DriverName);
dsc.setUsername(username);
dsc.setPassword(password);
//使用mysql数据库
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
//pc.setModuleName(scanner("请输入模块名(存放的包名称 如system)"));
pc.setParent(parent);
pc.setController("controller");
pc.setService("service");
pc.setServiceImpl("service.impl");
pc.setMapper("mapper");
pc.setEntity("pojo");
pc.setXml("mapper");
mpg.setPackageInfo(pc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
//设置哪些表需要自动生成
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
//实体类名称驼峰命名
strategy.setNaming(NamingStrategy.underline_to_camel);
//列名名称驼峰命名
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
//设置controller的api风格 使用RestController
strategy.setRestControllerStyle(true);
//驼峰转连字符
strategy.setControllerMappingHyphenStyle(true);
//忽略表前缀
strategy.setTablePrefix(tablePrefix);
mpg.setStrategy(strategy);
mpg.execute();
}
}

1.3 代码生成器()

依赖
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
生成器代码
package edu.xja.springbootmp;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class CodeGeneration {
public static void main(String[] args) {
/**
* 先配置数据源
*/
MySqlQuery mySqlQuery = new MySqlQuery() {
@Override
public String[] fieldCustom() {
return new String[]{"Default"};
}
};
DataSourceConfig dsc = new
DataSourceConfig.Builder("jdbc:mysql://localhost:3306/security?
&useUnicode=true&characterEncoding=utf-
8&serverTimezone=Asia/Shanghai","root","123456")
.dbQuery(mySqlQuery).build();
//通过datasourceConfig创建AutoGenerator
AutoGenerator generator = new AutoGenerator(dsc);
Scanner scanner = new Scanner(System.in);
System.out.println("代码生成的绝对路径(右键项目->copy path):");
String projectPath = scanner.next();
System.out.println("请输入表名,多个英文逗号分隔,所有输入 all");
String s = scanner.next();
/**
* 全局配置
*/
//String projectPath = System.getProperty("user.dir"); //获取项目路径
String filePath = projectPath + "/src/main/java"; //java下的文件路径
GlobalConfig global = new GlobalConfig.Builder()
.outputDir(filePath)//生成的输出路径
.author("sunwz")//生成的作者名字
//.enableSwagger()开启swagger,需要添加swagger依赖并配置
.dateType(DateType.TIME_PACK)//时间策略
.commentDate("yyyy年MM月dd日")//格式化时间格式
.disableOpenDir()//禁止打开输出目录,默认false
.fileOverride()//覆盖生成文件
.build();
/**
* 包配置
*/
PackageConfig packages = new PackageConfig.Builder()
.entity("pojo")//实体类包名
.parent("edu.xja")//父包名。如果为空,将下面子包名必须写全部, 否则就只需写子
包名
.controller("controller")//控制层包名
.mapper("mapper")//mapper层包名
.xml("mapper.xml")//数据访问层xml包名
.service("service")//service层包名
.serviceImpl("service.impl")//service实现类包名
//.other("output")//输出自定义文件时的包名
.pathInfo(Collections.singletonMap(OutputFile.xml, projectPath +
"/src/main/resources/mapper")) //路径配置信息,就是配置各个文件模板的路径信息,这里以
mapper.xml为例
.build();
/**
* 模板配置
*/
// 如果模板引擎是 freemarker
// String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm";
TemplateConfig template = new TemplateConfig.Builder()
// .disable()//禁用所有模板
//.disable(TemplateType.ENTITY)禁用指定模板
// .service(filePath + "/service.java")//service模板路径
// .serviceImpl(filePath + "/service/impl/serviceImpl.java")//实现类模
板路径
// .mapper(filePath + "/mapper.java")//mapper模板路径
// .mapperXml("/templates/mapper.xml")//xml文件模板路路径
// .controller(filePath + "/controller")//controller层模板路径
.build();
/**
* 注入配置,自定义配置一个Map对象
*/
// Map<String,Object> map = new HashMap<>();
// map.put("name","young");
// map.put("age","22");
// map.put("sex","男");
// map.put("description","深情不及黎治跃");
//
// InjectionConfig injectionConfig = new InjectionConfig.Builder()
// .customMap(map)
// .build();
/**
* 策略配置开始
*/
StrategyConfig strategyConfig = new StrategyConfig.Builder()
.enableCapitalMode()//开启全局大写命名
//.likeTable()模糊表匹配
.addInclude(getTables(s))//添加表匹配,指定要生成的数据表名,不写默认选定数据
库所有表
.addTablePrefix("tb_", "sys_") //设置忽略表前缀
//.disableSqlFilter()禁用sql过滤:默认(不使用该方法)true
//.enableSchema()启用schema:默认false
.entityBuilder() //实体策略配置
//.disableSerialVersionUID()禁用生成SerialVersionUID:默认true
.enableChainModel()//开启链式模型
.enableLombok()//开启lombok
.enableRemoveIsPrefix()//开启 Boolean 类型字段移除 is 前缀
.enableTableFieldAnnotation()//开启生成实体时生成字段注解
//.addTableFills()添加表字段填充
.naming(NamingStrategy.underline_to_camel)//数据表映射实体命名策略:默认
下划线转驼峰underline_to_camel
.columnNaming(NamingStrategy.underline_to_camel)//表字段映射实体属性命名
规则:默认null,不指定按照naming执行
.idType(IdType.AUTO)//添加全局主键类型
.formatFileName("%s")//格式化实体名称,%s取消首字母I
.build()
.mapperBuilder()//mapper文件策略
.enableMapperAnnotation()//开启mapper注解
.enableBaseResultMap()//启用xml文件中的BaseResultMap 生成
.enableBaseColumnList()//启用xml文件中的BaseColumnList
//.cache(缓存类.class)设置缓存实现类
.formatMapperFileName("%sMapper")//格式化Dao类名称
.formatXmlFileName("%sMapper")//格式化xml文件名称
.build()
.serviceBuilder()//service文件策略
.formatServiceFileName("%sService")//格式化 service 接口文件名称
.formatServiceImplFileName("%sServiceImpl")//格式化 service 接口文件名
称
.build()
.controllerBuilder()//控制层策略
//.enableHyphenStyle()开启驼峰转连字符,默认:false
.enableRestStyle()//开启生成@RestController
.formatFileName("%sController")//格式化文件名称
.build();
/*至此,策略配置才算基本完成!*/
/**
* 将所有配置项整合到AutoGenerator中进行执行
*/
generator.global(global)
.template(template)
// .injection(injectionConfig)
.packageInfo(packages)
.strategy(strategyConfig)
.execute();
}
// 处理 all 情况
protected static List<String> getTables(String tables) {
return "all".equals(tables) ? Collections.emptyList() :
Arrays.asList(tables.split(","));
}
}

1.4 配置文件

1.5 Rest测试MP应用  

package edu.xja.controller;
import edu.xja.pojo.User;
import edu.xja.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* <p>
* 用户表 前端控制器
* </p>
*
* @author sunwz
* @since 2023年04月06日
*/
@RestController // restFul + @RestController = @Controller + @ResponseBody
/*@Controller
@ResponseBody*/
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
//一旦使用@RestController表示当前项目是一个前后端分离的项目,这里没有作用域返回数据全是json
/**
* 查询全部 @GetMapping
* id查询 @GetMapping
* 增加 @PostMapping
* 修改 @PutMapping
* 删除 @DeleteMapping
*/
@GetMapping
public List<User> list(){
return userService.list();
}
@GetMapping("/{id}")
public User byId(@PathVariable("id") Long id){
return userService.getById(id);
}
@PostMapping
public boolean save(@RequestBody User user){
return userService.save(user);
}
@PutMapping
public boolean update(@RequestBody User user){
return userService.updateById(user);
}
@DeleteMapping("/{id}")
public boolean delete(@PathVariable Long id){
return userService.removeById(id);
}
}

1.6 关于时间处理

旧版生成日期 java.util.Date
/**
* 注册时间
*/
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date registerTime;
新版生成日期 import java.time.LocalDateTime;
Java 8 有一个全新的日期和时间 API 。 此 API 中最有用的类之一是 LocalDateTime
@TableField("create_time")
private LocalDateTime createTime;
但是新版会出现 JSON 转化时间问题,因此我们添加一个配置类整体解决这个问题
@Configuration
public class LocalDateTimeConfig {
/*
* 序列化内容
* LocalDateTime -> String
* 服务端返回给客户端内容
* */
@Bean
public LocalDateTimeSerializer localDateTimeSerializer() {
return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd
HH:mm:ss"));
}
/*
* 反序列化内容
* String -> LocalDateTime
* 客户端传入服务端数据
* */
@Bean
public LocalDateTimeDeserializer localDateTimeDeserializer() {
return new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd
HH:mm:ss"));
}
// 配置
@Bean
public Jackson2ObjectMapperBuilderCustomizer
jackson2ObjectMapperBuilderCustomizer() {
return builder -> {
1.7 案例
2. SpringBoot自动配置原理
SpringBoot 虽然干掉了 XML 但未做到 零配置,它体现出了一种 约定优于配置,也称作按约定编程,是一种
软件设计范式,旨在减少软件开发人员需做决定的数量,获得简单的好处,而又不失灵活性。 一般情况下默
认的配置足够满足日常开发所需,但在特殊的情况下,我们往往需要用到自定义属性配置、自定义文件配
置、多环境配置、外部命令引导等一系列功能。不用担心,这些 SpringBoot 都替我们考虑好了,我们只需
要遵循它的规则配置即可
2.1 Spring Boot的配置文件
初识Spring Boot时我们就知道,Spring Boot有一个全局配置文件:application.properties或
application.yml。
我们的各种属性都可以在这个文件中进行配置,最常配置的比如:server.port、logging.level.* 等等,然而
我们实际用到的往往只是很少的一部分,那么这些属性是否有据可依呢?答案当然是肯定的,这些属性都可
以在官方文档中查找到,但是开发这是比较喜欢记忆一些常用的配置,或者使用idea提供功能进行一些相关
的配置
那么问题来了:这些配置是如何在Spring Boot项目中生效的呢? 那么接下来,就需要聚焦本篇博客的主
题:自动配置工作原理或者叫实现方式。
2.2 工作原理剖析
Spring Boot关于自动配置的源码在spring-boot-autoconfigure-x.x.x.x.jar中:
builder.serializerByType(LocalDateTime.class,
localDateTimeSerializer());
builder.deserializerByType(LocalDateTime.class,
localDateTimeDeserializer());
};
}
}

2. SpringBoot自动配置原理

SpringBoot 虽然干掉了 XML 但未做到 零配置,它体现出了一种 约定优于配置,也称作按约定编程,是一种软件设计范式,旨在减少软件开发人员需做决定的数量,获得简单的好处,而又不失灵活性。 一般情况下默认的配置足够满足日常开发所需,但在特殊的情况下,我们往往需要用到自定义属性配置、自定义文件配置、多环境配置、外部命令引导等一系列功能。不用担心,这些 SpringBoot 都替我们考虑好了,我们只需要遵循它的规则配置即可

2.1 Spring Boot的配置文件

初识 Spring Boot 时我们就知道, Spring Boot 有一个全局配置文件: application.properties
application.yml
我们的各种属性都可以在这个文件中进行配置,最常配置的比如: server.port logging.level.* 等等,然而 我们实际用到的往往只是很少的一部分,那么这些属性是否有据可依呢?答案当然是肯定的,这些属性都可 以在官方文档中查找到,但是开发这是比较喜欢记忆一些常用的配置,或者使用idea 提供功能进行一些相关的配置
那么问题来了: 这些配置是如何在 Spring Boot 项目中生效的呢? 那么接下来,就需要聚焦本篇博客的主题:自动配置工作原理或者叫实现方式。

2.2 工作原理剖析

Spring Boot 关于自动配置的源码在 spring-boot-autoconfigure-x.x.x.x.jar 中:
自动配置原理的相关描述,官方文档没有提及。不过我们不难猜出, Spring Boot 的启动类上有一个 @SpringBootApplication注解,这个注解是 Spring Boot 项目必不可少的注解。那么自动配置原理一定和这个注解有着千丝万缕的联系!
@SpringBootApplication 是一个复合注解或派生注解,在 @SpringBootApplication 中有一个注解
@EnableAutoConfiguration ,翻译成人话就是开启自动配置,其定义如下:

 

而这个注解也是一个派生注解,其中的关键功能由 @Import 提供,其导入的
AutoConfigurationImportSelector selectImports() 方法通过
SpringFactoriesLoader.loadFactoryNames() 扫描所有具有 META-INF/spring.factories jar 包。
spring-boot-autoconfigure-x.x.x.x.jar 里就有一个这样的 spring.factories 文件。
这个 spring.factories 文件也是一组一组的 key=value 的形式,其中一个 key
EnableAutoConfiguration 类的全类名,而它的 value 是一个 xxxxAutoConfiguration 的类名的列表,这些类名以逗号分隔,如下图所示:

这个 @EnableAutoConfiguration 注解通过 @SpringBootApplication 被间接的标记在了 Spring Boot 的启动 类上。在 SpringApplication.run(...) 的内部就会执行 selectImports() 方法,找到所有 JavaConfig 自动配置类的全限定名对应的class ,然后将所有自动配置类加载到 Spring 容器中。

2.3 自动配置生效原理

每一个 XxxxAutoConfiguration 自动配置类都是在某些条件之下才会生效的,这些条件的限制在 Spring Boot中以注解的形式体现,常见的条件注解有如下几项:
@ConditionalOnBean :当容器里有指定的 bean 的条件下。
@ConditionalOnMissingBean :当容器里不存在指定 bean 的条件下。
@ConditionalOnClass :当类路径下有指定类的条件下。
@ConditionalOnMissingClass :当类路径下不存在指定类的条件下。
@ConditionalOnProperty :指定的属性是否有指定的值,比如
@ConditionalOnProperties(prefix=”xxx.xxx”, value=”enable”, matchIfMissing=true)
代表当 xxx.xxx enable 时条件的布尔值为 true ,如果没有设置的情况下也为 true
ServletWebServerFactoryAutoConfiguration 配置类为例,解释一下全局配置文件中的属性如何生效,比如: server.port=8081 ,是如何生效的(当然不配置也会有默认值,这个默认值来自于
org.apache.catalina.startup.Tomcat )。
ServletWebServerFactoryAutoConfiguration 类上,有一个 @EnableConfigurationProperties 注解:开启配置属性,而它后面的参数是一个ServerProperties 类,这就是习惯优于配置(该做的帮你做好) 的最终落地点。

 

在这个类上,我们看到了一个非常熟悉的注解: @ConfigurationProperties ,它的作用就是从配置文件中绑定属性到对应的bean 上,而 @EnableConfigurationProperties 负责导入这个已经绑定了属性的 bean 到spring容器中(见上面截图)。那么所有其他的和这个类相关的属性都可以在全局配置文件中定义,也就是说,真正“ 限制 我们可以在全局配置文件中配置哪些属性的类就是这些 XxxxProperties 类,它与配置文件中定义的prefix 关键字开头的一组属性是唯一对应的。
至此,我们大致可以了解。在全局配置的属性如: server.port 等,通过 @ConfigurationProperties 注解,绑定到对应的XxxxProperties 配置实体类上封装为一个 bean ,然后再通@EnableConfigurationProperties 注解导入到 Spring 容器中。
而诸多的 XxxxAutoConfiguration 自动配置类,就是 Spring 容器的 JavaConfig( 俗称 Java 配置 ) 形式,作用就是为Spring 容器导入 bean ,而所有导入的 bean 所需要的属性都通过 xxxxProperties bean 来获得。
可能到目前为止还是有所疑惑,但面试的时候,其实远远不需要回答的这么具体,你只需要这样回答 ( 背下来)

https://cloud.tencent.com/developer/article/2120900 

https://blog.csdn.net/weixin_45934981/article/details/129877810

 3. SpringBoot上传

文件上传和下载是 JAVA WEB 中常见的一种操作,文件上传主要是将文件通过 IO 流传输到服务器的某一个特定的文件夹下;刚开始工作那会一个上传文件常常花费小半天的时间,繁琐的代码量以及XML 配置让我是痛不欲生;值得庆幸的是有了Spring Boot 短短的几句代码就能实现文件上传与本地写入操作 ….

3.1 导入依赖

在 pom.xml 中添加上 spring-boot-starter-web 和 spring-boot-starter-thymeleaf 的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

3.2 配置文件

默认情况下 Spring Boot 无需做任何配置也能实现文件上传的功能,但有可能因默认配置不符而导致文件上传失败问题,所以了解相关配置信息更有助于我们对问题的定位和修复;
# 禁用 thymeleaf 缓存
spring.thymeleaf.cache=false
# 是否支持批量上传 (默认值 true)
spring.servlet.multipart.enabled=true
# 上传文件的临时目录 (一般情况下不用特意修改)
spring.servlet.multipart.location=
# 上传文件最大为 1M (默认值 1M 根据自身业务自行控制即可)
spring.servlet.multipart.max-file-size=1048576
# 上传请求最大为 10M(默认值10M 根据自身业务自行控制即可)
spring.servlet.multipart.max-request-size=10485760
# 文件大小阈值,当大于这个阈值时将写入到磁盘,否则存在内存中,(默认值0 一般情况下不用特意修改)
spring.servlet.multipart.file-size-threshold=0
# 判断是否要延迟解析文件(相当于懒加载,一般情况下不用特意修改)
spring.servlet.multipart.resolve-lazily=false

3.3 上传页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>评论详情</title>
<!--<link rel="stylesheet" href="../static/css/bootstrap.min.css">
<script src="../static/js/bootstrap.min.js"></script>
<script src="../static/js/jquery-3.6.0.js"></script>
<script src="../static/js/My97DatePicker/WdatePicker.js"></script>-->
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}">
<script type="text/javascript" th:src="@{/js/jquery-3.6.0.js}"></script>
<script type="text/javascript" th:src="@{/js/bootstrap.min.js}"></script>
<script type="text/javascript" th:src="@{/js/My97DatePicker/WdatePicker.js}">
</script>
<style>
.files span{
cursor: pointer;
}
</style>
<script>
$(function () {
/* 这种方式能够默认提供新生内容事件 */
$(document).on("click" ,".add", function () {
$("#files").append("<div><input type=\"file\" name=\"file\"
style=\"margin-top: 3px;display: inline\"> <span class=\"add\"> 增加 </span><span
class=\"del\"> 删除 </span></div>");
})
$(document).on("click" ,".del", function () {
$(this).parent().remove();
})
})
</script>
</head>
<body>
<div class="container">
<form style="margin-top: 20px" action="/comment/saveComment"
enctype="multipart/form-data" method="post">
<div class="form-group">
<label for="userId">用户</label>
<select class="form-control" id="userId" name="userId" style="width:
200px">
<option></option>
<option th:each="user:${list}" th:value="${user.id}"
th:text="${user.username}"></option>
</select>
</div>
<div class="form-group">
<label for="content">评论信息</label>
<textarea class="form-control" rows="3" id="content" name="content"
style="width: 600px"></textarea>
</div>
<div class="form-group">
<label for="score">评分</label>
<input type="text" class="form-control" id="score" name="score"
style="width: 100px" >
</div>
<div class="form-group files" id="files">
<label >上传图片</label>
<div><input type="file" name="file" style="margin-top: 3px;display:
inline"> <span class="add"> 增加 </span><span class="del"> 删除 </span></div>
</div>
<div class="form-group">
<label for="publicDate">上传时间</label>
<input type="text" class="form-control Wdate" id="publicDate"
name="publicDate" style="width: 200px" onclick="WdatePicker({el:this,dateFmt:'yyyyMM-dd HH:mm:ss'})">
</div>
<button type="submit" class="btn btn-success">提交评论</button>
</form>
</div>
</body>
</html>

3.4 控制层

创建一个 FileUploadController ,其中 @GetMapping 的方法用来跳转 index.html 页面,而
@PostMapping 相关方法则是对应的 单文件上传 、多文件上传 ** 处理方式。
@RequestParam("file")* 此处的 "file" 对应的就是 html name="file" input 标签,而将文件真正写入的还是借助的commons-io 中的 FileUtils.copyInputStreamToFile(inputStream,file)
@Controller
@RequestMapping("/uploads")
public class FileUploadController {
private static final Logger log =
LoggerFactory.getLogger(FileUploadController.class);
@GetMapping
public String index() {
return "index";
}
@PostMapping("/upload1")
@ResponseBody
public Map<String, String> upload1(@RequestParam("file") MultipartFile file)
throws IOException {
3.5 配置tomcat虚拟目录
log.info("[文件类型] - [{}]", file.getContentType());
log.info("[文件名称] - [{}]", file.getOriginalFilename());
log.info("[文件大小] - [{}]", file.getSize());
// TODO 将文件写入到指定目录(具体开发中有可能是将文件写入到云存储/或者指定目录通过 Nginx
进行 gzip 压缩和反向代理,此处只是为了演示故将地址写成本地电脑指定目录)
file.transferTo(new File("F:\\app\\chapter16\\" +
file.getOriginalFilename()));
Map<String, String> result = new HashMap<>(16);
result.put("contentType", file.getContentType());
result.put("fileName", file.getOriginalFilename());
result.put("fileSize", file.getSize() + "");
return result;
}
@PostMapping("/upload2")
@ResponseBody
public List<Map<String, String>> upload2(@RequestParam("file") MultipartFile[]
files) throws IOException {
if (files == null || files.length == 0) {
return null;
}
List<Map<String, String>> results = new ArrayList<>();
for (MultipartFile file : files) {
// TODO Spring Mvc 提供的写入方式
file.transferTo(new File("F:\\app\\chapter16\\" +
file.getOriginalFilename()));
Map<String, String> map = new HashMap<>(16);
map.put("contentType", file.getContentType());
map.put("fileName", file.getOriginalFilename());
map.put("fileSize", file.getSize() + "");
results.add(map);
}
return results;
}
}

3.5 配置tomcat虚拟目录

package com.xja.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 配置tomcat下的虚拟目录:核心思想就将本地的文件夹当成项目资源用tomcat的形式提供访问
*/
@Configuration
//@Component
public class TomcatDocResource implements WebMvcConfigurer {
4. SpringBoot常用接口
4.1 ApplicationListener
ApplicationListener可以监听某个事件event,通过实现这个接口,传入一个泛型事件,在run方法中就可以
监听这个事件,从而做出一定的逻辑,比如在等所有bean加载完之后执行某些操作. Spring典型的观察者设
计模式。
同时这个事件还可以直接读取到IOC容器中所有被实例化的对象
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//addResourceHandler 配置访问路径
//addResourceLocations 配置资源的具体地址
//虽然能使用但是影响项目原有的请求
//registry.addResourceHandler("/**").addResourceLocations("file:D:\\comment_image\\
");
registry.addResourceHandler("/ims/**").addResourceLocations("file:D:\\comment_image
\\");
}
}
配置欢迎页
//欢迎页
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//直接跳转
//registry.addViewController("/").setViewName("ww.html");
//需要向后台查询之后在进页面
registry.addViewController("/").setViewName("redirect:/user/toWelcome");
}

4. SpringBoot常用接口

4.1 ApplicationListener

ApplicationListener 可以监听某个事件 event ,通过实现这个接口,传入一个泛型事件,在 run 方法中就可以监听这个事件,从而做出一定的逻辑,比如在等所有bean 加载完之后执行某些操作 . Spring 典型的观察者设计模式。
同时这个事件还可以直接读取到 IOC 容器中所有被实例化的对象
@Component
public class SystemListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (event.getApplicationContext().getParent() == null) {
System.out.println("do something");
// 打印容器中出事Bean的数量
System.out.println("监听器获得容器中初始化Bean数量:" +
event.getApplicationContext().getBeanDefinitionCount());
}
}
}
Spring内置事件
1 ContextRefreshedEvent
ApplicationContext 被初始化或刷新时,该事件被发布。这也可以在 ConfigurableApplicationContext 接口中使用 refresh() 方法来发生。此处的初始化是指:所有的 Bean 被成功装载,后处理 Bean 被检测并激活,所 有Singleton Bean 被预实例化, ApplicationContext 容器已就绪可用
2 ContextStartedEvent
当使用 ConfigurableApplicationContext ApplicationContext 子接口)接口中的 start() 方法启动
ApplicationContext 时,该事件被发布。你可以调查你的数据库,或者你可以在接受到这个事件后重启任何停止的应用程序
3 ContextStoppedEvent
当使用 ConfigurableApplicationContext 接口中的 stop() 停止 ApplicationContext 时,发布这个事件。你可以在接受到这个事件后做必要的清理的工作
4 ContextClosedEvent
当使用 ConfigurableApplicationContext 接口中的 close() 方法关闭 ApplicationContext 时,该事件被发布。一个已关闭的上下文到达生命周期末端;它不能被刷新或重启
5 RequestHandledEvent
这是一个 web-specific 事件,告诉所有 bean HTTP 请求已经被服务。只能应用于使用 DispatcherServlet 的Web应用。在使用 Spring 作为前端的 MVC 控制器时,当 Spring 处理用户请求结束后,系统会自动触发该事件比较实用的场景:比如有一个超大全球性的电商平台,当我购买完产品之后需要通知用户我们准备发货了。 正常思想是当支付完成之后直接在支付的业务后面调用短信和邮箱的接口实现通知的业务,但是作为一个大的平台面向全球,那么通知的方式未来也可能有很多种方式,我们也不能在支付的业务后面调用太多的通知的接口,为了能够实现业务解耦合,我们可以定义一个支付成功的事件,时间发布成功之后可以通过监听获 取,然后再进行多种形式的通知,这样既不影响原有的支付业务,而且代码的拓展能力也变强了。 这种发布订阅( 的模式 ) 和后续学习的 mq 的机制同理,当然这种业务也完全可以通过 mq 来实现。

4.2 ApplicationContextAware

接口可以获取 Spring 容器对象 , 可以获取容器中的 Bean
@Component
public class PayService2 implements ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws
BeansException {
// 初始化applicationContext对象
this.applicationContext = applicationContext;
}
}

4.3 InitializingBean

此接口的类拥有初始化 Bean 的功能,实际开发中我们可以直接使用 @@Component 直接初始化

4.4 HandlerInterceptor

我们实现接口所创建的拦截器并不会自动生效
需要将自定义拦截器放入实现了 WebMvcConfigurer 接口的配置类中
@Log4j2
@Component
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse
response, Object handler) throws Exception {
log.warn("我已经成功进入到拦截器。。。。。");
return true;
}
}
配置拦截器
@Component
public class MyHandle implements WebMvcConfigurer {
@Autowired
private LoginHandlerInterceptor loginHandlerInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginHandlerInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/user/login");
}
}

4.5 WebMvcConfigurer

WebMvcConfigurer 配置类其实是 Spring 内部的一种配置方式,采用 JavaBean 的形式来代替传统的 xml 配置文件形式进行针对框架个性化定制,可以自定义一些Handler Interceptor ViewResolver ,MessageConverter。基于 java-based 方式的 spring mvc 配置,需要创建一个配置类并实现WebMvcConfigurer 接口;
Spring Boot 1.5 版本都是靠重写 WebMvcConfigurerAdapter 的方法来添加自定义拦截器,消息转换器等。SpringBoot 2.0 后,该类被标记为 @Deprecated (弃用)。官方推荐直接实现 WebMvcConfigurer 或者直接继承WebMvcConfigurationSupport
其中必须掌握常用方法包括 :
/* 拦截器配置 */
void addInterceptors(InterceptorRegistry var1);
/* 视图跳转控制器 */
void addViewControllers(ViewControllerRegistry registry);
/** *静态资源处理**/
void addResourceHandlers(ResourceHandlerRegistry registry);
/* 默认静态资源处理器 */
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
/* 这里配置视图解析器 */
void configureViewResolvers(ViewResolverRegistry registry);
/* 配置内容裁决的一些选项*/
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
/** 解决跨域问题 **/
public void addCorsMappings(CorsRegistry registry) ;

4.5.1 addInterceptors

addInterceptor :需要一个实现 HandlerInterceptor 接口的拦截器实例
addPathPatterns :用于设置拦截器的过滤路径规则; addPathPatterns("/**") 对所有请求都拦截
excludePathPatterns :用于设置不需要拦截的过滤规则
拦截器主要用途:进行用户登录状态的拦截,日志的拦截等
@Override
public void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry);
registry.addInterceptor(new
TestInterceptor()).addPathPatterns("/**").excludePathPatterns("/emp/toLogin","/emp/l
ogin","/js/**","/css/**","/images/**");
}

4.5.2 addViewControllers

以前写 SpringMVC 的时候,如果需要访问一个页面,必须要写 Controller 类,然后再写一个方法跳转到页面,感觉好麻烦,其实重写WebMvcConfigurer 中的 addViewControllers 方法即可达到效果了
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//配置一个路径定位到指定的视图上。
registry.addViewController("/toLogin").setViewName("login");
//配置欢迎页面
registry.addViewController("/").setViewName("login.html");
registry.addViewController("/").setViewName("forward:/login/login1");
}
值的指出的是,在这里重写 addViewControllers 方法,并不会覆盖 WebMvcAutoConfiguration
Springboot 自动配置)中的 addViewControllers (在此方法中, Spring Boot “/” 映射至 index.html ), 这也就意味着自己的配置和Spring Boot 的自动配置同时有效,这也是我们推荐添加自己的 MVC 配置的方式。

4.5.3 addResourceHandlers

正常情况下,静态资源都是放在 static 下面,是不需要处理的。
比如,我们想自定义静态资源映射目录的话,只需重写 addResourceHandlers 方法即可。
@Configuration
public class MyWebMvcConfigurerAdapter implements WebMvcConfigurer {
/**
* 配置静态访问资源
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//放行静态资源
registry.addResourceHandler("/mycss/**").addResourceLocations("classpath:/mycss/");
//tomcat虚拟目录
registry.addResourceHandler("/images/**").addResourceLocations("file:D://123");
}
}
addResoureHandler :指的是对外暴露的访问路径
addResourceLocations :指的是内部文件放置的目录

4.5.4 静态资源处理器

默认静态资源处理器 configureDefaultServletHandling
<mvc:default-servlet-handle>
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer
configurer) {
configurer.enable();
configurer.enable("defaultServletName");
}

4.5.5 视图解析器

configureViewResolvers
这个方法是用来配置视图解析器的,该方法的参数 ViewResolverRegistry 是一个注册器,用来注册你想自定义的视图解析器等。
/**
* 配置请求视图映射
* @return
*/
@Bean
public InternalResourceViewResolver resourceViewResolver()
{
InternalResourceViewResolver internalResourceViewResolver = new
InternalResourceViewResolver();
//请求视图文件的前缀地址
internalResourceViewResolver.setPrefix("/WEB-INF/jsp/");
//请求视图文件的后缀
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver;
}
/**
* 视图配置
* @param registry
*/
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
super.configureViewResolvers(registry);
registry.viewResolver(resourceViewResolver());
}

5. Swagger

接口文档对于前后端开发人员都十分重要。尤其近几年流行前后端分离后,接口文档又变成重中之重。接口文档固然重要,但是由于项目周期等原因,后端人员经常出现无法及时更新,导致前端人员抱怨接口文档和实际情况不一致。
很多人员会抱怨别人写的接口文档不规范,不及时更新。但是,当自己写的时候,确实最烦去写接口文档。 这种痛苦只有亲身经历才会牢记于心
如果接口文档可以实时动态生成就不会出现上面问题。
Swagger 可以完美的解决上面的问题。
优点
自动生成文档,只需要在接口中使用注解进行标注,就能生成对应的接口文档。
自动更新文档,由于是动态生成的,所以如果你修改了接口,文档也会自动对应修改(如果你也更新了注解的话)。这样就不会出现我修改了接口,却忘记更新接口文档的情况。
支持在线调试, swagger 提供了在线调用接口的功能。
缺点
不能创建测试用例,所以他暂时不能帮你处理完所有的事情。他只能提供一个简单的在线调试,如果你想存储你的测试用例,可以使用Postman 或者 YAPI 这样支持创建测试用户的功能。
要遵循一些规范,它不是任意规范的。比如说,你可能会返回一个 json 数据,而这个数据可能是一个 Map 格式的,那么我们此时不能标注这个Map 格式的返回数据的每个字段的说明,而如果它是一个实体类的话,我们可以通过标注类的属性来给返回字段加说明。也比如说,对于swagger ,不推荐在使用 GET 方式提交数据的时候还使用Body ,仅推荐使用 query 参数、 header 参数或者路径参数,当然了这个限制只适用于在线调试。
没有接口文档更新管理,虽然一个接口更新之后,可能不会关心旧版的接口信息,但你 可能 想看看旧版的接口信息,例如有些灰度更新发布的时候可能还会关心旧版的接口。那么此时只能由后端去看看有没有注释留下了,所以可以考虑接口文档大更新的时候注释旧版的,然后写下新版的。
代码中存在注解依赖

5.1 整合swagger

依赖
<!--swagger依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency
要使用 swagger ,我们必须对 swagger 进行配置,我们需要创建一个 swagger 的配置类,比如可以命名为 SwaggerConfig.java
package com.its.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/*
* 用于配置SwaggerApi
* */
//开启Swagger使用(项目注释文档)
@EnableSwagger2
//标明是配置类
@Configuration
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
//用于生成API信息
.apiInfo(apiInfo())
//select()函数返回一个ApiSelectorBuilder实例,用来控制接口被swagger做成文档
.select()
//用于指定扫描哪个包下的接口
.apis(RequestHandlerSelectors.basePackage("com.its.controller"))
//选择所有的API,如果你想只为部分API生成文档,可以配置这里
.paths(PathSelectors.any())
.build();
}
/*
*用于定义API主界面的信息,比如可以声明所有的API的总标题、描述、版本
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//用来自定义API的标题
.title("SpringBoot项目SwaggerAPI")
//用来描述整体的API
.description("SpringBoot项目SwaggerAPI描述测试")
//创建人信息
.contact(new Contact("sunwz","http://localhost:8080/swaggerui.html","xxxxxxxx@163.com"))
//用于定义服务的域名
//.termsOfServiceUrl("")
.version("1.0") //可以用来定义版本
.build();
}
}
配置文件添加 (SpringBoot2.5 以上都需要添加 )
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
运行我们的 Spring Boot 项目,(我默认是 8080 端口,如果你不一样,请注意修改后续的 url ),
访问 http://localhost:8080/swagger-ui.html
然后你就可以看到一个如下的界面,由于我们暂时没有配置接口数据,所以下面显示 No operations defined in spec!

5.2 定义接口组  

使用了 @Api 来标注一个 Controller 之后,如果下面有接口,那么就会默认生成文档,但没有我们自定义的说明:
package com.its.swagger.controller;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "用户管理")
@RestController
public class UserController {
// 注意,对于swagger,不要使用@RequestMapping,
// 因为@RequestMapping支持任意请求方式,swagger会为这个接口生成7种请求方式的接口文档
@GetMapping("/info")
public String info(String id){
return "aaa";
}
}
package com.its.swagger.controller;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(tags = "角色管理") // tags:你可以当作是这个组的名字。
@RestController
public class RoleController {
@PostMapping("/ww")
public boolean ww(){
return false;
}
}
重启查看 UI 视图

视图解析

 

 

我们可以使用 @ApiOperation 来描述接口,比如:

@ApiOperation(value = "用户测试",notes = "用户测试notes")
@GetMapping("/test")
public String test(String id){
return "test";
}

5.3 定义接口请求参数

上面使用了 @ApiOperation 来了描述接口,但其实还缺少接口请求参数的说明,下面我们分场景来讲。注意一下,对于GET 方式, swagger 不推荐使用 body 方式来传递数据,也就是不希望在 GET 方式时使用json、 form-data 等方式来传递,这时候最好使用路径参数或者 url 参数。 ( 虽然 POSTMAN 等是支持的 ) ,所以如果接口传递的数据是json 或者 form-data 方式的,还是使用 POST 方式好。

5.3.1 请求参数是实体类

此时我们需要使用 @ApiModel 来标注实体类,然后在接口中定义入参为实体类即可:
@ApiModel :用来标类
常用配置项:
        1. value:实体类简称
        2. description:实体类说明
@ApiModelProperty :用来描述类的字段的意义。
         常用配置项:
        1. value:字段说明
        2. example:设置请求示例( Example Value )的默认值,如果不配置,当字段为 string 的时候,此时请求示例中默认值为"".
        3. name:用新的字段名来替代旧的字段名。
        4. allowableValues:限制值得范围,例如 {1,2,3} 代表只能取这三个值; [1,5] 代表取 1 5 的值; (1,5) 代表 1到5 的值,不包括 1 5 ;还可以使用 infinity -infinity 来无限值,比如 [1, infinity] 代表最小值为 1 ,最大 值无穷大。
public class Filter {
@ApiModelProperty(allowableValues = "range[1,5]")
Integer order
@ApiModelProperty(allowableValues = "111, 222")
String code;
}
1. required :标记字段是否必填,默认是 false,
2. hidden :用来隐藏字段,默认是 false ,如果要隐藏需要使用 true ,因为字段默认都会显示,就算没有@ApiModelProperty。
package com.its.swagger.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
// 先使用@ApiModel来标注类
@ApiModel(value="用户登录表单对象",description="用户登录表单对象")
@Data //getter,setter,swagger也需要这个
public class LoginForm {
// 使用ApiModelProperty来标注字段属性。
@ApiModelProperty(value = "用户名",required = true,example = "root")
private String username;
@ApiModelProperty(value = "密码",required = true,example = "123456")
private String password;
}
定义成入参:
@ApiOperation(value = "登录接口",notes = "登录接口的说明")
@PostMapping("/login")
public LoginForm login(@RequestBody LoginForm loginForm){
return loginForm;
}

5.3.2 请求参数是非实体类

5.3.2.1 声明入参是URL参数

再说一次:对于 GET 方式, swagger 不推荐使用 body 方式来传递数据,所以虽然 Spring MVC 可以自动封装参数,但对于GET 请求还是不要使用 form-data json 等方式传递参数,除非你使用 Postman 来测试接口,swagger在线测试是不支持这个操作的。
对于非实体类参数,可以使用 @ApiImplicitParams @ApiImplicitParam 来声明请求参数。
@ApiImplicitParams 用在方法头上, @ApiImplicitParam 定义在 @ApiImplicitParams 里面,一个
@ApiImplicitParam 对应一个参数。
@ApiImplicitParam 常用配置项:
        1. name:用来定义参数的名字,也就是字段的名字 , 可以与接口的入参名对应。如果不对应,也会生成,所以可以用来定义额外参数!
        2. value:用来描述参数
        3. required:用来标注参数是否必填
        4. paramType有 path,query,body,form,header 等方式,但对于对于非实体类参数的时候,常用的只有path,query,header; body form 是不常用的。 body 不适用于多个零散参数的情况,只适用于 json 对 象等情况。【如果你的接口是form-data,x-www-form-urlencoded 的时候可能不能使用 swagger 页面 API调试,但可以在后面讲到基于 BootstrapUI swagger 增强中调试,基于 BootstrapUI swagger 支持指定form-data x-www-form-urlencoded
// 使用URL query参数
@ApiOperation(value = "登录接口2",notes = "登录接口的说明2")
@ApiImplicitParams({
@ApiImplicitParam(name = "username",//参数名字
value = "用户名",//参数的描述
required = true,//是否必须传入
//paramType定义参数传递类型:有path,query,body,form,header
paramType = "query"
)
,
@ApiImplicitParam(name = "password",//参数名字
value = "密码",//参数的描述
required = true,//是否必须传入
paramType = "query"
)
})
@PostMapping(value = "/login2")
public LoginForm login2(String username,String password){
System.out.println(username+":"+password);
LoginForm loginForm = new LoginForm();
loginForm.setUsername(username);
loginForm.setPassword(password);
return loginForm;
}

5.3.2.2 声明入参是URL路径参数

// 使用路径参数
@PostMapping("/login3/{id1}/{id2}")
@ApiOperation(value = "登录接口3",notes = "登录接口的说明3")
@ApiImplicitParams({
@ApiImplicitParam(name = "id1",//参数名字
value = "用户名",//参数的描述
required = true,//是否必须传入
//paramType定义参数传递类型:有path,query,body,form,header
paramType = "path"
)
,
@ApiImplicitParam(name = "id2",//参数名字
value = "密码",//参数的描述
required = true,//是否必须传入
paramType = "path"
)
})
public String login3(@PathVariable Integer id1,@PathVariable Integer id2){
return id1+":"+id2;
}

5.3.2.3 声明入参是header参数

// 用header传递参数
@PostMapping("/login4")
@ApiOperation(value = "登录接口4",notes = "登录接口的说明4")
@ApiImplicitParams({
@ApiImplicitParam(name = "username",//参数名字
value = "用户名",//参数的描述
required = true,//是否必须传入
//paramType定义参数传递类型:有path,query,body,form,header
paramType = "header"
)
,
@ApiImplicitParam(name = "password",//参数名字
value = "密码",//参数的描述
required = true,//是否必须传入
paramType = "header"
)
})
public String login4( @RequestHeader String username,
@RequestHeader String password){
return username+":"+password;
}

5.3.2.4 声明文件上传参数

// 有文件上传时要用@ApiParam,用法基本与@ApiImplicitParam一样,不过@ApiParam用在参数上
// 或者你也可以不注解,swagger会自动生成说明
@ApiOperation(value = "上传文件",notes = "上传文件")
@PostMapping(value = "/upload")
public String upload(@ApiParam(value = "图片文件", required = true)MultipartFile
uploadFile){
String originalFilename = uploadFile.getOriginalFilename();
return originalFilename;
}
// 多个文件上传时,**swagger只能测试单文件上传**
@ApiOperation(value = "上传多个文件",notes = "上传多个文件")
@PostMapping(value = "/upload2",consumes = "multipart/*", headers = "contenttype=multipart/form-data")
public String upload2(@ApiParam(value = "图片文件", required = true,allowMultiple =
true)MultipartFile[] uploadFile){
StringBuffer sb = new StringBuffer();
for (int i = 0; i < uploadFile.length; i++) {
System.out.println(uploadFile[i].getOriginalFilename());
sb.append(uploadFile[i].getOriginalFilename());
sb.append(",");
}
return sb.toString();
}
// 既有文件,又有参数
@ApiOperation(value = "既有文件,又有参数",notes = "既有文件,又有参数")
@PostMapping(value = "/upload3")
@ApiImplicitParams({
@ApiImplicitParam(name = "name",
value = "图片新名字",
required = true
)
})
public String upload3(@ApiParam(value = "图片文件", required = true)MultipartFile
uploadFile,
String name){
String originalFilename = uploadFile.getOriginalFilename();
return originalFilename+":"+name;
}

5.4 定义接口响应

定义接口响应,是方便查看接口文档的人能够知道接口返回的数据的意义。

5.4.1 响应是实体类

前面在定义接口请求参数的时候有提到使用 @ApiModel 来标注类,如果接口返回了这个类,那么这个类上的说明也会作为响应的说明:
// 返回被@ApiModel标注的类对象
@ApiOperation(value = "实体类响应",notes = "返回数据为实体类的接口")
@PostMapping("/role1")
public LoginForm role1(@RequestBody LoginForm loginForm){
return loginForm;
}

5.4.2 响应是非实体类 

swagger 无法对非实体类的响应进行详细说明,只能标注响应码等信息。是通过 @ApiResponses
@ApiResponse 来实现的。
@ApiResponses @ApiResponse 可以与 @ApiModel 一起使用。
// 其他类型的,此时不能增加字段注释,所以其实swagger推荐使用实体类
@ApiOperation(value = "非实体类",notes = "非实体类")
@ApiResponses({
@ApiResponse(code=200,message = "调用成功"),
@ApiResponse(code=401,message = "无权限" )
5.5 Swagger UI增强
你可能会觉得现在这个UI不是很好看,现在有一些第三方提供了一些Swagger UI增强,比较流行的是
swagger-bootstrap-ui
在swagger配置类中增加注解 @EnableSwaggerBootstrapUI :
}
)
@PostMapping("/role2")
public String role2(){
return " {\n" +
" name:\"广东\",\n" +
" citys:{\n" +
" city:[\"广州\",\"深圳\",\"珠海\"]\n" +
" }\n" +
" }";
}

5.5 Swagger UI增强 

你可能会觉得现在这个 UI 不是很好看,现在有一些第三方提供了一些 Swagger UI 增强,比较流行的是
swagger-bootstrap-ui
<!-- 引入swagger-bootstrap-ui依赖包-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
swagger 配置类中增加注解 @EnableSwaggerBootstrapUI :
@Configuration // 标明是配置类
@EnableSwagger2 //开启swagger功能
@EnableSwaggerBootstrapUI // 开启SwaggerBootstrapUI
public class SwaggerConfig {
// 省略配置内容
}
访问 API http://localhost:8080/doc.html ,即可预览到基于 bootstarp Swagger UI 界面。

5.6 整合Spring Security注意

Spring Boot 整合 Spring Security Swagger 的时候,需要配置拦截的路径和放行的路径,注意是放行以下几个路径。
.antMatchers("/swagger**/**").permitAll()
.antMatchers("/webjars/**").permitAll()
.antMatchers("/v2/**").permitAll()
.antMatchers("/doc.html").permitAll() // 如果你用了bootstarp的Swagger UI界面,加一个这
个。

6. @Configuration@Component

这两个注解有一个公共的功能就是能够实当前例化组件信息。如果在当前类中使用 @Bean 去再次注册组件信息的时候,如果使用
@Configuration(5.2 新增 ) 的注解, bean 的示例及时发生调用关系, bean 的组件依然是单例的,底层使用动态代理实现。 如果使用@Component 表示当前 bean 注册的组件一旦发生调用关系, bean 就不在是单例的。
应用:
如果作为配置类,我们经常会在类中使用 @Bean 声明对象,此时要使用 @Configuration
如果知识单纯的想将当前组件注入到容器中,类中没有 @Bean 。 使用 @Component
案例代码:
创建第一个实体类
package edu.xja.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
/**
* <p>
*
* </p>
*
* @author sunwz
* @since 2023年04月10日
*/
@Getter
@Setter
@Accessors(chain = true)
@TableName("cargo")
@ApiModel(value = "Cargo对象", description = "")
public class Cargo implements Serializable {
private static final long serialVersionUID = 1L;
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty(value = "名字",example = "小猪佩奇玩偶",allowableValues =
"range[3,10]",required = true)
@TableField("name")
private String name;
@TableField("unit")
private String unit;
@TableField("num")
private Integer num;
@TableField("price")
private Double price;
@TableField("manufacturer")
private String manufacturer;
@TableField("classify")
private Integer classify;
@TableField("time")
private LocalDateTime time;
@TableField("operator")
private String operator;
}
第二实体类
package edu.xja.pojo;
import lombok.Data;
/**
* <p>
*
* </p>
*
* @author sunwz
* @version 1.0
* @since 2023/4/10 14:11
*/
@Data
public class Student {
private String name = "张三";
private Cargo cargo;
}
测试 @Configuration
package edu.xja.ex;
import edu.xja.pojo.Cargo;
import edu.xja.pojo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* <p>
*
* </p>
*
* @author sunwz
* @version 1.0
* @since 2023/4/10 13:45
*/
@Configuration
public class ConfigurationTest {
private String id = "10010";
public String getId() {
return id;
}
/*@Bean
public Student student(){
Student student = new Student();
student.setCargo(cargo());
return student;
}
@Bean
public Cargo cargo(){
return new Cargo().setId(1).setName("哈哈");
}*/
}
测试 @Component
package edu.xja.ex;
import edu.xja.pojo.Cargo;
import edu.xja.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
* <p>
*
* </p>
*
* @author sunwz
* @version 1.0
* @since 2023/4/10 13:45
*/
@Component
public class ComponentTest {
private String id = "10086";
public String getId() {
return id;
}
@Bean
public Cargo cargo(){
return new Cargo().setId(1).setName("哈哈");
}
@Bean
public Student student(){
Student student = new Student();
//此处报错不影响运行,使用@Bean注释的方法被直接调用。请使用依赖项注入。检查信息:报告错误使
用代理方法的警告。
// Spring Framework 5.2引入了@Configuration类处理的优化,
// 可以通过属性@Configuration(proxyBeanMethods = false)来启用。
// 如果你禁用proxyBeanMethods不再创建代理实例,
// 并且调用该方法会再次调用它(每次返回一个新实例)。
// 因此,您无法保证您实际上在上下文中注入了相应的bean。
student.setCargo(cargo());
return student;
}
}
打印的数据
package edu.xja;
import edu.xja.ex.ComponentTest;
import edu.xja.ex.ConfigurationTest;
import edu.xja.pojo.Cargo;
import edu.xja.pojo.Student;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SwaggerApplication {
public static void main(String[] args) {
ApplicationContext run = SpringApplication.run(SwaggerApplication.class,
args);
ComponentTest test1 = run.getBean(ComponentTest.class);
System.out.println("test1 = " + test1.getId());
ConfigurationTest test2 = run.getBean(ConfigurationTest.class);
System.out.println("test2 = " + test2.getId());
//读取配置类创建对象
Cargo cargo = run.getBean(Cargo.class);
Student student = run.getBean(Student.class);
System.out.println(cargo);
System.out.println(student.getCargo());
}
}

(1)
Spring Boot启动的时候类上有一个注解@SpringBootApplication,
程序通过SpringApplication.run()将本类的字节码传递到这个类中,从通过反射机制加载这个注解。
 @SpringBootApplication是一个派生注解,点击去发现 @EnableAutoConfiguration这就是开启自动配置的注解,
通过这个注解找到@Improt  导入的注解找到   META-INF/spring.factories配置文件中的所有自动配置类,并对其进行加载
,而这些自动配置类都是以AutoConfiguration结尾来命名的,它实际上就是一个JavaConfig形式的Spring容器配置类,
它能通过以Properties结尾命名的类中取得在全局配置文件中配置的属性如:server.port,
而XxxxProperties类是通过@ConfigurationProperties注解与全局配置文件中对应的属性进行绑定的,这样就完成了一个自动配置功能。
(2)
主配置类启动,通过@SringBootApplication 中的@EnableAutoConfguration 加载所需的所 有自动配置类,
然后自动配置类生效并给容器添加各种组件。那么@EnableAutoConfguration 其实是通过它里面的@AutoConfigurationPackage 注解,
将主配置类的所在包皮下面所有子包 里面的所有组件扫描加载到 Spring 容器中; 
还通过@EnableAutoConfguration 里面的 AutoConfigurationImportSelector 选择器中的 SringFactoriesLoader.loadFactoryNames()方法,
获取类路径下的 META-INF/spring.factories 中的 资源并经过一些列判断之后作为自动配置类生效到容器中,自动配置类生效后帮我们进行自 动配置工作,
就会给容器中添加各种组件:这些组件的属性是从对应的 Properties 类中获取 的,
这些 Properties 类里面的属性又是通过@ConfigurationProperties 和配置文件绑定的:所以 我们能配置的属性也都是来源于这个功能的 Properties 类。
SpringBoot 在自动配置很多组件 的时候,先判断容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置 的,
如果没有,才自动配置;如果有些组件可以有多个就将用户配置和默认配置的组合起来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值