1.
加入swagger的依赖
2
一定要有SwaggerConfig配置类
具体例如:
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket createRestApi(ApiInfo apiInfo) { return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo) .groupName("SwaggerGroupOneAPI") .select() .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) .paths(PathSelectors.any()) .build(); } @Bean public ApiInfo getinfo(){ ApiInfo apiInfo = new ApiInfo("小萌神测试", "测试", "1.1.0", "http://www.baidu.com", "LE", "一", "http:www.hyycinfo.com/g1"); return apiInfo; } /** * 增加如下配置可解决Spring Boot 6.x 与Swagger 3.0.0 不兼容问题 **/ @Bean public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) { List<ExposableEndpoint<?>> allEndpoints = new ArrayList(); Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints(); allEndpoints.addAll(webEndpoints); allEndpoints.addAll(servletEndpointsSupplier.getEndpoints()); allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints()); String basePath = webEndpointProperties.getBasePath(); EndpointMapping endpointMapping = new EndpointMapping(basePath); boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath); return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null); } private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) { return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT)); } }
3
配置swagger的路径匹配方法
4
配置spring boot主类时加上@EnableSwagger2注解