Springboot整合swagger3

在pom.xml里引入swagger3的依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot_xzit</artifactId>
        <groupId>com.xzit</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>day2_job</artifactId>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- 引入swagger3(springfox)的依赖 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>
</project>

创建一个主类,加入springboot的注解

package com.xzit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.oas.annotations.EnableOpenApi;

@SpringBootApplication
@EnableOpenApi
public class Day2server {
    public static void main(String[] args) {
        SpringApplication.run(Day2server.class,args);
    }
}

编写swagger配置类

package com.xzit.config;

import io.swagger.annotations.Api;
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;

@Configuration
public class Swagger3Config {
    @Bean
    public Docket apiConfig(){
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                //设置通过什么方式定位到需要生成文档的接口
                //定位了方法上的ApiOperation
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                //接口URL路径,any表示全部路径
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("伊万的测试项目")
                .description("项目描述信息")
                .contact(new Contact("伊万","http://www.baidu.com","ussa@qq.com"))
                .version("1.0")
                .build();
    }
}

创建一个自定义的application.yml配置文件

ant_path_matcher是swagger的配置
banner.txt是自定义springboot启动时显示的头图
restart: exclude是定义如static等文件夹里静态资源的变化不触发springboot服务的自动重启
spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
  banner:
    location: banner.txt
  devtools:
    restart:
      exclude: static/**,public/**,templates/**

创建一个Teacher类

package com.xzit.model;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "老师对象",description = "老师对象,用来对应数据库表teacher")
public class Teacher {
    @ApiModelProperty(value = "老师id",required = true,example = "1")
    private int id;
    @ApiModelProperty(value = "老师姓名",required = true,example = "伊万老师")
    private String name;
    @ApiModelProperty(value = "老师性别",required = true,example = "男")
    private String gender;
    @ApiModelProperty(value = "老师年龄",required = false,example = "26")
    private int age;
}

创建一个controller类

package com.xzit.controller;

import com.xzit.model.Teacher;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/teacher")
@Api(tags = {"老师管理"})//用在类上,表示对类的说明tags表示说明该类的作用
public class TeacherController {
    @GetMapping()
    @ApiOperation(value = "显示全部老师数据")
    //用在方法上,说明方法的用途和作用
    public Object list(){
        List<Teacher> list=new ArrayList<>();
        list.add(new Teacher(1,"廖老师","女",29));
        list.add(new Teacher(2,"王老师","男",35));
        list.add(new Teacher(3,"李老师","男",31));
        list.add(new Teacher(4,"张老师","女",29));
        return list;
    }

    @GetMapping("/{id}")
    @ApiOperation("按给定Id来查询老师信息")
    @ApiImplicitParams(
            @ApiImplicitParam(
                    name = "id",value = "老师id",required = true,
                    paramType = "path",
                    dataType = "Integer",dataTypeClass = Integer.class
            )
    )

    public Object selectById(@PathVariable int id){
        return new Teacher(id,"郭老师","男",22);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("按给定ID来删除老师信息")
    public Object deleteById(@ApiParam(name = "id",value = "老师id") @PathVariable int id){
        return "已删除给定的id为"+id+"的老师对象";
    }

    @PostMapping()
    @ApiOperation("新增老师")
    public Object save(@ApiParam(name = "teacher",value = "老师对象")@RequestBody Teacher teacher){
        return teacher;
    }

    @PutMapping()
    @ApiOperation("更新老师")
    public Object update(@RequestBody Teacher teacher){
        return teacher;
    }
}

 

posted @ 2022-08-03 23:19  伊万  阅读(274)  评论(0编辑  收藏  举报