92、DevTools依赖实现热部署(仅在开发环境下生效)

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
  <scope>runtime</scope>
  <optional>true</optional>
</dependency>

Settings-Compiler-Build Project automatically

# 配置thymeleaf缓存为false
spring:
  thymeleaf:
    cache: false

93、指定不同环境的配置文件

# 此配置在application.yml中
spring:
  profiles:
    active: prod # 与application.yml文件同级目录下新建 application-prod.yml

94、自定义随机端口

server:  port: ${random.int[9000,9100]}

95、mvn package后的jar包执行时报找不到manifest文件错误

参考链接

<!-- 需要增加如下编译插件 -->
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <version>2.4.3</version>
      <!-- 增加如下配置,对新生成的jar重新打包 -->
      <executions>
        <execution>
          <goals>
            <goal>repackage</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

96、读取properties配置文件的方式

<!--spring-boot-configuration-processor依赖-->
<dependency>      
	<groupId>org.springframework.boot</groupId>      
	<artifactId>spring-boot-configuration-processor</artifactId>      		
	<optional>true</optional>
</dependency>
# mail.properties
mail.username=wjh
mail.password=wjh123
// 新建读取mail.properties配置文件的Java类
@Data
@Configuration
// ignoreResourceNotFound:指定的配置文件不存在是否报错,默认是false。当设置为 true 时,若该文件不存在,程序不会报错。实际项目开发中,最好设置 ignoreResourceNotFound 为 false。 
@PropertySource(value = {"classpath:mail.properties"},
        ignoreResourceNotFound = false, encoding = "UTF-8", name = "mail.properties")
@ConfigurationProperties(prefix = MailConfig.PREFIX, ignoreInvalidFields = true)
public class MailConfig {
    // 对应配置文件中的mail前缀
    public static final String PREFIX = "mail"; 
 
    private String username;
    private String password;
}

97、yml定义的属性用EL表达式${} 引用

bridge:
  file:
    BasePath: /var/robot/bridge
    voltage: ${bridge.file.BasePath}/voltage

98、UserAgent 获取请求信息的方法

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
String operatingSystem = userAgent.getOperatingSystem().toString();
String browserType = userAgent.getBrowser().toString();

99、springboot发送邮件

开启POP3和SMTP协议,需要获得邮件服务器的授权码

引入邮件依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
# 配置邮箱基本信息
spring:
  mail:
    # 配置 SMTP 服务器地址
    host: smtp.qq.com
    # 发送者邮箱
    username: 790933839@qq.com
    # 配置密码,注意不是真正的密码,而是刚刚申请到的授权码
    password: vjstfghblprwbdbd
    # 端口号465或587
    port: 587 
    # 默认的邮件编码为UTF-8
    default-encoding: UTF-8
    # 配置SSL 加密工厂
    properties:
      mail:
        smtp:
          socketFactoryClass: javax.net.ssl.SSLSocketFactory
        #表示开启 DEBUG 模式,这样,邮件发送过程的日志会在控制台打印出来,方便排查错误
        debug: true

126邮箱SMTP服务器地址:smtp.126.com,端口号:465或者994
163邮箱SMTP服务器地址:smtp.163.com,端口号:465或者994
yeah邮箱SMTP服务器地址:smtp.yeah.net,端口号:465或者994
qq邮箱SMTP服务器地址:smtp.qq.com,端口号465或587*
配置好这些后,springboot会自动帮我们配置好相关的邮件发送类。

以下是三种发送邮件的方式

// 第一种:发送简单邮件
@Autowired
JavaMailSender javaMailSender;

/**
     * 测试发送简单邮件
     */
@Test
public void testSimpleMailSend() {
  // 构建一个简单邮件对象
  SimpleMailMessage message = new SimpleMailMessage();
  // 设置邮件主题
  message.setSubject("这是一封测试邮件的主题");
  // 设置邮件发送者,这个跟application.yml中设置的要一致
  message.setFrom("953975179@qq.com");
  // 设置邮件接收者,可以有多个接收者,中间用逗号隔开,以下类似
  // message.setTo("10*****16@qq.com","12****32*qq.com");
  message.setTo("stallonely@163.com");
  // 设置邮件抄送人,可以有多个抄送人
  // message.setCc("12****32*qq.com");
  // 设置隐秘抄送人,可以有多个
  // message.setBcc("7******9@qq.com");
  // 设置邮件发送日期
  message.setSentDate(new Date());
  // 设置邮件的正文
  message.setText("这是测试邮件的正文");
  // 发送邮件
  javaMailSender.send(message);
}

// 第二种:发送Mime复杂邮件
/**
     * 测试发送Mime复杂邮件
     *
     * @throws MessagingException
     */
@Test
public void testMimeMailSend() throws MessagingException {
  // 构建一个Mime邮件对象
  MimeMessage mimeMessage = javaMailSender.createMimeMessage();
  // 构建一个Mime邮件对象助手 true:构建一个可以带附件的邮件对象助手
  MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

  // 设置邮件主题
  helper.setSubject("<p style='color:red;'>这是一封测试邮件的主题</p>");
  helper.setFrom("953975179@qq.com");
  helper.setTo("stallonely@163.com");
  helper.setSentDate(new Date());

  boolean containsHtml = true;
  // 设置邮件正文,可以是html5标签
  helper.setText(
    "<p style='color:blue;'>这是测试邮件的正文</p>" +
    "<p>第一张图片:</p><img src='cid:p01'/>" +
    "<p>第二张图片:</p><img src='cid:p02'/>", containsHtml);
  // 第一个参数是自定义的名称,后缀需要加上,第二个参数是文件的位置
  helper.addInline("p01", new FileSystemResource(new File("/Users/KXY/Desktop/1.jpg")));
  helper.addInline("p02", new FileSystemResource(new File("/Users/KXY/Desktop/2.jpg")));
  helper.addAttachment("交接说明-孔向云.md", new File("/Users/KXY/Desktop/交接说明-孔向云.md"));
  javaMailSender.send(mimeMessage);
}
第三种:发送Thymeleaf模板邮件

<dependency>    
  <groupId>org.springframework.boot</groupId>    
  <artifactId>spring-boot-starter-thymeleaf</artifactId> 
</dependency>
@Autowired
TemplateEngine templateEngine;

/**
     * 测试发送Thymeleaf模板邮件
          * @throws MessagingException
          */
@Test
public void sendThymeleafMail() throws MessagingException {
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
    helper.setSubject("这是一封测试邮件的主题");
    helper.setFrom("953975179@qq.com");
    helper.setTo("stallonely@163.com");
    helper.setSentDate(new Date());
    // Template的Context
    Context context = new Context();
    // 设置模板中的变量
    context.setVariable("username", "javaboy");
    context.setVariable("num","000001");
    context.setVariable("salary", "99999");
    // 第一个参数为模板的名称
    String process = templateEngine.process("mail_template", context);
    // 第二个参数true表示这是一个html文本
    helper.setText(process,true);
    javaMailSender.send(mimeMessage);
}

100、Springboot整合 swagger2

Swagger2 是一个规范和完整的框架,用于生成、描述、调用和可视化Restful风格的web服务
Swagger2 用来帮助我们整理接口信息的,我们通过Swagger提供的注解,来对接口和model进行描述。
Swagger2 可以提供功能测试

Swagger2相关的依赖

<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>

SwaggerConfig配置类

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.Contact;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration@EnableSwagger2public class SwaggerConfig {  /**  配置一个Docket Bean这个Bean中,配置映射路径和要扫描的接口的位置,在apiInfo中,主要配置一下Swagger2文档网站的信息,例如网站的title,网站的描述,联系人的信息,使用的协议等等。这样,Swagger2就算是配置成功了。要注意修改里面的扫描包的位置。位置随意。  */  @Bean  public Docket createKlockDocket() {    return new Docket(DocumentationType.SWAGGER_2)      .apiInfo(apiInfo())      .pathMapping("/")      .select()      .apis(RequestHandlerSelectors.basePackage("com.ants.controller"))      .paths(PathSelectors.any())      .build();  }  /**     * 自定义ApiInfo     *     * @return 自定义的ApiInfo实例     */  private ApiInfo apiInfo() {    // 作者信息    Contact contact = new Contact("孔向云",  "http://www.klock.cn/", "stallonely@163.com");    return new ApiInfo("KLOCK接口API文档", "文档详细信息查询", "v1.0", "http://www.klock.cn/",                       contact , "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<VendorExtension>());  }  /**     * 通过 ApiInfoBuilder 自定义ApiInfo     *     * @return 自定义的ApiInfo实例     */  private ApiInfo apiInfoByBuilder() {    // 作者信息    Contact contact = new Contact("孔向云",  "http://www.klock.cn/", "stallonely@163.com");    return new ApiInfoBuilder().title("KLOCK接口API文档")      .description("文档详细信息查询")      .version("v1.0")      .termsOfServiceUrl("http://www.klock.cn/")      .contact(contact)      .license("The Apache License 2.0")      .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")      .extensions(new ArrayList<VendorExtension>())      .build();  }}

启动项目,并访问http://localhost:8080/swagger-ui.html该页面可以调试api,类似Postman

如果项目配置了拦截器,需要放开一些请求如下
.excludePathPatterns("/swagger-resources/", "/webjars/", "/v2/", "/swagger-ui.html/")

添加Swagger相关注解

/**给Controller类添加注解1.在类上添加@Api注解来对类和方法进行描述2.方法上添加@ApiOperation注解来对方法进行描述3.方法上添加@ApiImplicitParams注解来对方法参数进行描述。@ApiImplicitParam注解中虽然可以指定参数是必填的,但是却不能代替@RequestParam(required = true),前者的必填只是在Swagger2框架内必填,抛弃了Swagger2,这个限制就没用了,所以假如开发者需要指定一个参数必填,@RequestParam(required = true)注解还是不能省略。*/@RestController@RequestMapping(value = "/test")@Api(tags = "用户crud测试")public class UserController {  @Autowired  private UserService userService;  /**     * Swagger测试     */  @PostMapping("/testSwagger")  @ApiOperation("Swagger的测试")  @ApiImplicitParams({    @ApiImplicitParam(name = "name", value = "用户名称", defaultValue="zhangsan"),    @ApiImplicitParam(name = "password", value = "用户密码", defaultValue="123456"),  })  public User getUserByid(@RequestParam(name = "name")String name, @RequestParam(name = "password")String password){    User user = new User();    user.setName(name);    user.setPassword(password);    return user;  }}/**在model类中使用@ApiModelProperty注解来描述各个属性*/package com.flyinghome.tm.model;import io.swagger.annotations.ApiModelProperty;import lombok.Data;@Datapublic class User {    @ApiModelProperty(value = "用户id")    private String id;    @ApiModelProperty(value = "用户姓名")    private String name;    @ApiModelProperty(value = "用户密码")    private String password;    @ApiModelProperty(value = "页码")    private String pageno;    @ApiModelProperty(value = "数量")    private String pagesize;}

参考:

  1. https://blog.csdn.net/u012702547/article/details/88775298

Q.E.D.


行走在天地间自由的灵魂