前言:

本文内容:Swagger分组和接口注释、异步任务、邮件任务

推荐免费SpringBoot基础教程视频:【狂神说Java】SpringBoot最新教程通俗易懂_哔哩哔哩_bilibili

Swagger分组和接口注释

分组

  1. SwaggerConfig类的Docket方法中进行配置

    1
    2
    // 设置组名
    .groupName("组名")
  2. SwaggerConfig设置多个组

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // 设置多个组
    // group A
    @Bean
    public Docket docket1(){
    return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }
    // group B
    @Bean
    public Docket docket2(){
    return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }
    // group C
    @Bean
    public Docket docket3(){
    return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }

接口注释

  1. 在HelloController中添加方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    // 只要我们在接口中,返回值中存在实体类,他就会被扫描到Swagger中
    @PostMapping(value="/user")
    public User user(){
    return new User();
    }

    // Operation接口 方法上
    @ApiOperation("Hello控制类")
    @GetMapping(value = "/hello2")
    public String hello2(@ApiParam("用户名") String username){
    return "hello"+username;
    }
  2. pojo包下新建User.java实体类,使用lombok

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    package com.jokerdig.pojo;

    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;

    /**
    * @author Joker大雄
    * @data 2022/7/29 - 12:36
    **/
    // @Api("注释")
    @ApiModel("用户实体类")
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class User {
    @ApiModelProperty("用户名")
    private String username;
    @ApiModelProperty("密码")
    private String password;
    }
  3. 运行测试

    1
    2
    3
    4
    5
    6
    7
    8
    Models
    用户实体类{
    password string
    密码

    username string
    用户名
    }

小结

  1. 可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息
  2. 接口文档实时更新
  3. 可以在线测试
  4. 正式发布一定要关闭Swagger

异步任务

  1. 新建项目springboot-09-asynchronous并勾选web依赖

  2. service包下新建AsyncService

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package com.jokerdig.service;

    import org.springframework.stereotype.Service;

    /**
    * @author Joker大雄
    * @data 2022/7/29 - 13:08
    **/
    @Service
    public class AsyncService {
    public void hello(){
    try {
    Thread.sleep(3000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println("数据正在处理...");
    }
    }
  3. controller包下新建AsyncController

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package com.jokerdig.controller;

    import com.jokerdig.service.AsyncService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    /**
    * @author Joker大雄
    * @data 2022/7/29 - 13:12
    **/
    @RestController
    public class AsyncController {
    @Autowired
    private AsyncService asyncService;

    @RequestMapping("/hello")
    public String hello(){
    asyncService.hello();
    return "ok";
    }
    }
  4. 运行测试

    http://localhost:8080/hello

    发现浏览器需要加载三秒后才能显示数据,我们要解决这种问题。

  5. AnsycServicehello方法上添加异步注解

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    package com.jokerdig.service;

    import org.springframework.scheduling.annotation.Async;
    import org.springframework.stereotype.Service;

    /**
    * @author Joker大雄
    * @data 2022/7/29 - 13:08
    **/
    @Service
    public class AsyncService {
    @Async // 这是一个异步任务
    public void hello(){
    try {
    Thread.sleep(3000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println("数据正在处理...");
    }
    }
  6. 在主类中开启异步

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    package com.jokerdig;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.scheduling.annotation.EnableAsync;

    // 开启异步任务注解
    @EnableAsync
    @SpringBootApplication
    public class Springboot09AsynchronousApplication {

    public static void main(String[] args) {
    SpringApplication.run(Springboot09AsynchronousApplication.class, args);
    }
    }
  7. 运行测试

    数据直接显示,并没有等待三秒

邮件任务

  1. pom中引入邮件依赖

    1
    2
    3
    4
    5
    <!--        javax.mail-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
  2. 在application中添加邮箱配置

    1
    2
    3
    4
    5
    spring.mail.username=jokerdaxiong@qq.com
    spring.mail.password=xxxxxxxx
    spring.mail.host=smtp.qq.com
    # qq邮箱需要开启加密验证
    spring.mail.properties.mail.smtl.ssl.enbale=true
  3. 在测试类中测试邮件发送

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    package com.jokerdig;

    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSenderImpl;
    import org.springframework.mail.javamail.MimeMessageHelper;

    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;
    import java.io.File;

    @SpringBootTest
    class Springboot09AsynchronousApplicationTests {

    // 注入邮件发送
    @Autowired
    JavaMailSenderImpl mailSender;

    // 简单邮件发送
    @Test
    void contextLoads() {
    SimpleMailMessage mailMessage = new SimpleMailMessage();
    // 题目
    mailMessage.setSubject("jokerdig.com");
    // 正文
    mailMessage.setText("这是springboot发送的邮件");
    // 接收人
    mailMessage.setTo("jokerdaxiong@qq.com");
    // 发送人
    mailMessage.setFrom("jokerdaxiong@qq.com");
    // 发送
    mailSender.send(mailMessage);

    }

    // 复杂邮件发送
    @Test
    void contextLoads2() throws MessagingException {
    // 创建一个复杂邮件
    MimeMessage mimeMessage = mailSender.createMimeMessage();
    // 组装复杂邮件
    MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true);
    message.setSubject("jokerdig.com复杂");
    message.setText("<p style='color:red'>这是springboot发送的带标签复杂邮件</p>",true);
    // 附件
    message.addAttachment("1.jpg",new File("1.jpg"));
    message.setTo("jokerdaxiong@qq.com");
    message.setFrom("jokerdaxiong@qq.com");
    // 发送
    mailSender.send(mimeMessage);
    }

    }
  4. 邮件发送完成