SpringBoot整合Swagger3

前言

swagger是啥,是干什么的,有什么用,我想在这里我就不用介绍了,下面直接代码演示。

添加依赖



    4.0.0
    com.inchlifc
    SwaggerDemo
    0.0.1-SNAPSHOT
    jar
    SwaggerDemo
    Demo project for Spring Boot
    
        8
    
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.9
    

    
        
            io.springfox
            springfox-spring-web
            3.0.0
        
        
            io.springfox
            springfox-boot-starter
            3.0.0
        


        
            org.springframework.boot
            spring-boot-starter-web
            3.1.3
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.1
            
        
    


 添加配置

package com.inchlifc.swaggerdemo.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * Swagger3的接口配置
 * 
 * @author chery
 */
@Configuration
public class SwaggerConfig{

    // 控制是否允许swagger
    private boolean enableSwagger = true;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30) //展示的swagger文档格
                .enable(enableSwagger) //是否启动swagger配置
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }


}

配置文件

spring:
    mvc:
     pathmatch:
      matching-strategy: ant_path_matcher

测试Controller

package com.inchlifc.swaggerdemo.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/swagger")
@Api(value = "测试",tags = {"测试Swagger"})
public class TestController {

    @ApiOperation(value = "测试接口")
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(){
        return "hello";
    }
}

启动演示

SpringBoot整合Swagger3_第1张图片

你可能感兴趣的:(#,Spring,Boot,spring,boot,后端,java)