springboot 2.7.2 集成swagger3

搭建项目:

  1. 创建spring boot项目,版本2.7.2
  2. jdk1.8

依赖引入:

        <!-- web依赖包-->
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- swagger3包-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <!-- 引入swagger-bootstrap-ui包,优化UI页面,可不加 -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

swagger配置:

配置文件配置:

server.port=8081
#true开启swagger false关闭
swagger.enabled=true
#Spring Boot 2.6.X使用的是PathPatternMatcher swagger想要正常使用需要添加
spring.mvc.pathmatch.matching-strategy=ant_path_matche

如果不配置:spring.mvc.pathmatch.matching-strategy=ant_path_matche
会出现异常;

rg.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper';

JAVA类配置

package com.example.demo.config;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author zjt
 * @description: swagger
 * @date 2022-07-15 16:04:24
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig {

    /**
     * application中还配置了mvc,因为springboot2.6.1与swagger3不兼容
     * ture 启用Swagger3.0, false 禁用(生产环境要禁用)
     */
    @Value("${swagger.enabled:false}")
    private Boolean swaggerEnabled;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                // 是否开启swagger
                .enable(swaggerEnabled)
                .select()
                // 扫描的路径使用@Api的controller
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 指定路径处理PathSelectors.any()代表所有的路径
                .paths(PathSelectors.any())
                .build();
                //请求URL都会加上/project前缀
                //.pathMapping("/project");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxx项目接口文档")
                .description("前后端分离的接口文档")
                .version("1.0")
                .build();
    }
    
    /**
     * 处理跨域问题
     * 允许跨域调用的过滤器
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        //允许所有域名进行跨域调用
        config.addAllowedOriginPattern("*");
        //允许跨越发送cookie
        config.setAllowCredentials(true);
        //放行全部原始头信息
        config.addAllowedHeader("*");
        //允许所有请求方法跨域调用
        config.addAllowedMethod("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}


测试Controller:

package com.example.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zjt
 * @description: swagger demo test
 * @date 2022-07-28 13:30:22
 */
@Api(tags = "测试接口")
@RestController
@Slf4j
@RequestMapping("/test")
public class TestController {

    @ApiOperation(value = "分页查询XXX信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "currentPage", value = "当前页码", dataTypeClass = Integer.class)
    })
    @GetMapping(value = "/selectPageList")
    public Map<String, Object> selectPageList(@RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                              @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage) {
        Map<String, Object> map = new HashMap<>();
        map.put("name","王某某");
        map.put("age",12);
        return map;
    }
}


启动项目访问:

新swagger UI界面: http://localhost:8081/doc.html
springboot 2.7.2 集成swagger3_第1张图片

旧swagger UI界面: http://localhost:8081/swagger-ui/index.html
springboot 2.7.2 集成swagger3_第2张图片

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