一、maven引入依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
</dependency>
二、编写配置类
@Configuration
@EnableSwagger2
@ConditionalOnExpression("${swagger.enable:true}")
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.directModelSubstitute(Long.class, String.class)
.directModelSubstitute(Integer.class,String.class)
.directModelSubstitute(BigDecimal.class,String.class)
.select()
.apis(RequestHandlerSelectors.basePackage("com.xx.xxx.xxx.controller"))
//.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("xx系统")
.description("xx系统 RESTful APIs")
// .termsOfServiceUrl("http://localhost:8090/")
.version("1.0")
.build();
}
}
三、启动服务
服务端口是port,查看swagger文档访问 http://ip:port/doc.html
即可
如果想导出文档可借助http://xiaoyaoji.cn/
导出文档参考文章:https://blog.csdn.net/zhangHP_123/article/details/121703158