- 引入依赖
-
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
- 配置类
-
package com.example.demo.config; import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties; import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties; import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType; import org.springframework.boot.actuate.endpoint.ExposableEndpoint; import org.springframework.boot.actuate.endpoint.web.*; import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier; import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier; import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; 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; import java.util.ArrayList; import java.util.Collection; import java.util.List; @Configuration @EnableOpenApi public class SwaggerConfig { /** * 创建API应用 * apiInfo() 增加API相关信息 * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现, * 本例采用指定扫描的包路径来定义指定要建立API的目录。 * swagger测试地址:http://127.0.0.1:8080/swagger-ui/index.html#/ * @return */ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build(); } /** * 创建该API的基本信息(这些基本信息会展现在文档页面中) * 访问地址:http://项目实际地址/swagger-ui.html * @return */ private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("SpringbootDemo项目") .description("用于测试返回界面的一些接口返回值") .termsOfServiceUrl("") .version("1.0") .build(); } /** * 增加如下配置可解决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)); } }
- 启动类
-
package com.example.demo; import com.example.demo.services.SyncImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.Banner; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.mybatis.spring.annotation.MapperScan; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import springfox.documentation.oas.annotations.EnableOpenApi; import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication @MapperScan("com.example.demo.mapper") @EnableScheduling @RestController @EnableSwagger2 @EnableOpenApi @EnableWebMvc public class App { protected static final Logger logger = LoggerFactory.getLogger(App.class); /** java -Daaaaa=1 -Dbbbbb=2 -Dspring.config.location=classes/application.yml -jar demo-0.0.1-SNAPSHOT.jar java -Daaaaa=1 -Dbbbbb=2 -Dspring.application.json="{\"logging\":{\"level\":{\"root\":\"off\"}}}" -jar demo-0.0.1-SNAPSHOT.jar java -Daaaaa=1 -Dbbbbb=2 -jar demo-0.0.1-SNAPSHOT.jar java -DsyncDingdan=1 -jar demo-0.0.1-SNAPSHOT.jar */ public static void main(String[] args) { SpringApplication.run(App.class, args); logger.info("服务启动了"); } }
- 测试代码:
-
@ApiOperation("获取老oa公司列表") @RequestMapping(value ="/list") public HashMap list(@RequestParam Map<String,Object> params){ Integer page = 1; Integer limit = 5; if(params.get("page") != null && params.get("page") != ""){ page = Integer.parseInt(params.get("page").toString()); if(page <= 0){ page = 1; } } }
- 效果:
- http://localhost:8091/swagger-ui/
-
遇到问题:
解决方案之‘Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPoi
参考:https://blog.csdn.net/hadues/article/details/123753888
标签:web,Springboot,boot,springframework,swagger3,整合,org,import,springfox From: https://www.cnblogs.com/xuxiaobo/p/17060321.html