自定义Swagger配置类,在springBoot项目中使用springBoot的注解,生成一个swagger配置的bean。 package com.yupi.usercenter.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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {@Bean(value = "defaultApi2") public Docket createRestApi(){return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.yupi.usercenter.controller")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("用户中心").contact(new Contact("whale", "www.xxx.com", "xxx@qq.com")).description("这是用Swagger动态生成的用户中心接口文档").termsOfServiceUrl("NO terms of service").version("1.0").build();}
}
若springboot versio>=2.6,需要添加以下配置 mvc:pathmatch:matching-strategy: ANT_PATH_MATCHER