swagger配置类

发布时间 2023-03-22 21:13:56作者: 小贤看世界

pom依赖

<!-- swagger2 配置 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
        </dependency>

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.6</version>
        </dependency>

swagger配置类

package com.javasm.config;

import com.javasm.common.http.EnumStatus;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//访问地址:http://localhost:8088/swagger-ui.html#

//使用com.github.xiaoymin依赖后
// http://localhost:8088/doc.html     原路径
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
    //api接口包扫描路径 
    public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.javasm";
    public static final String VERSION = "1.0.0";
    @Bean
    public Docket createRestApi() {

        //添加全局响应状态码
        List<ResponseMessage> responseMessageList = new ArrayList<>();
        Arrays.stream(EnumStatus.values()).forEach(enumStatus -> {
            responseMessageList.add(
                    new ResponseMessageBuilder().code(enumStatus.getStatus()).message(enumStatus.getMessage()).responseModel(
                            new ModelRef(enumStatus.getMessage())).build()
            );
        });
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .globalResponseMessage(RequestMethod.GET,responseMessageList)
                .globalResponseMessage(RequestMethod.DELETE,responseMessageList)
                .globalResponseMessage(RequestMethod.POST,responseMessageList)
                .globalResponseMessage(RequestMethod.PUT,responseMessageList)
                .select()
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
                .paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档,忽略哪些请求
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring boot  Api ") //设置文档的标题
                .description("Spring boot  Api 接口文档") // 设置文档的描述
                .version(VERSION) // 设置文档的版本信息-> 1.0.0 Version information
                .termsOfServiceUrl("http://www.xxx.com") //自己设置, 设置文档的License信息->1.3 License information
                .build();
    }

}

注解使用场景

@Api(value = "品牌模块", tags = {"品牌模块相关接口"})
@RestController
@RequestMapping("brand")
public class BrandController {

    @ApiOperation(value = "分页查询品牌", notes = "分页条件查询品牌信息", httpMethod = "GET")
    @GetMapping("findByPage")  // BrandCriteria : key=value 参数
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(
                            name = "brandName",
                            value = "品牌名称",
                            required = false,
                            dataType = "String",
                            paramType = "query"),
                    @ApiImplicitParam(
                            name = "currentPage",
                            value = "当前页",
                            required = true,
                            dataType = "Integer",
                            paramType = "query"
                    ),
                    @ApiImplicitParam(
                            name = "pageSize",
                            value = "每页大小",
                            required = true,
                            dataType = "Integer",
                            paramType = "query"
                    )

            })
    public AxiosResult findByPage(@ApiIgnore BrandCriteria brandCriteria) {
        PageResult<BrandVo> brandVoPageResult = brandService.findByPage(brandCriteria);
        return AxiosResult.success(brandVoPageResult);
    }