sprinvmvc整合swagger实现实时接口信息展示

1、pom.xml引入swagger插件

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<!-- 提供三种UI展示方式 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.github.caspar-chen</groupId>
<artifactId>swagger-ui-layer</artifactId>
<version>0.0.4</version>
</dependency>
<dependency>
<groupId>com.drore.cloud</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.4</version>
</dependency>

2、配置spring-mvc.xml注入swagger扫描配置

<!-- 整合swagger-UI -->
<bean class="com.ucredit.bc.common.filter.MySwaggerConfig"/>

3、创建MySwaggerConfig.java 配置中心实例

package com.**.common.filter;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableWebMvc
@EnableSwagger2
@ComponentScan(basePackages = {"com.**"})
@Configuration
public class MySwaggerConfig extends WebMvcConfigurerAdapter {

@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.**"))
.paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("**系统数据接口文档")
.termsOfServiceUrl("https://test-**.com")
.contact("**@**.com")
.version("0.0.1")
.description("**系统数据接口文档")
.build();
}

}

注:若工程使用的spring Shiro等安全拦截 需进行静态资源过滤,
/v2/** = anon
/doc.html = resource
/menu.json = resource
/docs.html = resource
/webjars/** = anon
/swagger-resources/** = anon
/swagger-ui.html = resource
原文地址:https://www.cnblogs.com/Gent-Wang/p/8944147.html