SpringBoot整合Swagger3

SpringBoot整合Swagger3

Swagger3可以给我们一个优美人性化的界面来访问和测试后端的接口,下面来看看SpringBoot中怎么整合Swagger3

1.导入依赖

Swagger依赖和其他组件信息


<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.5.1version>
    parent>
    <groupId>com.examplegroupId>
    <artifactId>mongoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.4.1version>
        dependency>

        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.1.16version>
        dependency>

        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.12version>
        dependency>

        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-generatorartifactId>
            <version>3.4.1version>
        dependency>

        
        <dependency>
            <groupId>org.apache.velocitygroupId>
            <artifactId>velocity-engine-coreartifactId>
            <version>2.3version>
        dependency>

        
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-boot-starterartifactId>
            <version>3.0.0version>
        dependency>


    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>


2.编写配置类

package com.example.mongo.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.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
@EnableOpenApi
public class SwaggerConfig {
    @Bean
    public Docket desertsApi(){
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.mongo.controller")) //这里需要写你需要扫描的包路径
                .paths(PathSelectors.any())
                .build()
                .groupName("default")
                .enable(true);
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("微服务 API说明文档")
                .description("微服务 API说明文档")
                .contact(new Contact("SpringBoot", "https://blog.csdn.net/BirdMan98", "[email protected]"))
                .termsOfServiceUrl("https://www.baidu.com/")
                .version("1.0")
                .build();
    }
}


3.测试验证

我们做完上面两个步骤后,重启服务,然后再浏览器访问地址:http://localhost/swagger-ui/index.html,注意我这里项目端口号改成了80,默认是8080

SpringBoot整合Swagger3_第1张图片

SpringBoot整合Swagger3_第2张图片

4.异常

如果出现异常在yml文件中添加如下配置

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

5.使用

在接口上使用@Api和@ApiOperation注解来对接口进行说明

SpringBoot整合Swagger3_第3张图片

你可能感兴趣的:(SpringBoot,spring,boot,mybatis,java)