第一步:增加jar包,引入最新版本2.9.2,访问的时候会报错java.lang.NumberFormatException,排除报错的包可以解决、也可以引入旧的jar包
<!-- 添加Swagger2依赖,用于生成接口文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<!-- 报错信息:java.lang.NumberFormatException: For input string: “” 排除jar包引用旧的jar包解决问题 -->
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引用旧的jar 排除新jar包中的问题 start -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
引入旧jar包
<!-- 添加Swagger2依赖,用于生成接口文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!--end-->
第二步:配置Configuration配置
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
//api接口包扫描路径
public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.cn.auth.controller.census";
public static final String VERSION = "1.0.0";
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
.paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档,忽略哪些请求
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("权限管理系统") //设置文档的标题
.description("权限管理系统API接口文档") //设置文档的描述
.version(VERSION) //设置文档的版本信息
.termsOfServiceUrl("localhost:8088") // 设置文档的License信息
.build();
}
}
第三步:配置shiro权限过滤
filterChainDefinitionMap.put("/swagger-ui.html","anon");
filterChainDefinitionMap.put("/v2/*","anon");
filterChainDefinitionMap.put("/swagger-resources/**","anon");
第四步:方法描述示例
@Api(description = "示例文档API接口")
@RestController
@RequestMapping("/swagger_example")
public class SwaggerExample {
@RequestMapping(value ="getTableList",method=RequestMethod.POST)
@ApiOperation(value="获取数据库表的列表")
@ApiImplicitParams({
@ApiImplicitParam(name="tableName",value="数据库表名",required=false,paramType="form",dataType="String"),
@ApiImplicitParam(name="page",value="第几页",required=true,paramType="form",dataType="int"),
@ApiImplicitParam(name="floatExp",value="传float类型数据",required=false,paramType="form",dataType="float"),
@ApiImplicitParam(name="doubleExp",value="传double类型数据",required=false,paramType="form",dataType="double"),
@ApiImplicitParam(name="booleanExp",value="传boolean类型数据",required=false,paramType="form",dataType="boolean"),
@ApiImplicitParam(name="limit",value="每页多少条数据",required=true,paramType="form",dataType="int")})
public BasePageData getTableList(String tableName, Integer page, Integer limit) {
BasePageData data = new BasePageData();
data.setMsg("操作成功");
return data;
}
}
第四步:请求地址出现接口页面
http://localhost:8088/swagger-ui.html
来源:oschina
链接:https://my.oschina.net/u/3204029/blog/3206675