声明!声明!声明!如果没有做其他配置的话,按照下方的配置肯定可以用,但是如果有其他配置的话我就不敢保证了,Swagger迭代的挺快的,不同版本之间的配置也略有不同。
第一步:引入依赖注意:直接复制,不要修改版本
io.springfox springfox-swagger22.9.2
com.github.xiaoymin swagger-bootstrap-ui1.9.6
第二步:写个swagger的配置类io.swagger swagger-annotations1.5.21 io.swagger swagger-models1.5.21
注意:标红的地方自己修改
package com.example.demo.config;
import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlbasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
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;
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(documentationType.SWAGGER_2)
//是否开启(true 开启,false()。生产环境建议隐藏)
.apiInfo(apiInfo())
.select().apis(RequestHandlerSelectors
.basePackage("com.example.demo.web"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//设置文档标题(API名称)
.title("设备功能接口文档")
//文档描述
.description("接口说明")
//服务条款URL
.termsOfServiceUrl("http://localhost:8899/")
//版本号
.version("1.0")
.build();
}
@Bean
public CorsFilter corsConfig(){
final UrlbasedCorsConfigurationSource urlbasedCorsConfigurationSource = new UrlbasedCorsConfigurationSource();
final CorsConfiguration corsConfiguration = new CorsConfiguration();
//是否允许请求带有验证
corsConfiguration.setAllowCredentials(true);
//允许访问的客户端域名
corsConfiguration.addAllowedOrigin("*");
//允许服务端访问的客户端请求
corsConfiguration.addAllowedHeader("*");
//允许访问的方法名,GET,POST
corsConfiguration.addAllowedMethod("*");
urlbasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
return new CorsFilter(urlbasedCorsConfigurationSource);
}
}
第三步:看下图
打开swagger页面http://192.168.31.25:8899/doc.html
勾选后保存
第四步:看下图注意画红圈的两个地方,第一个ApiSort是用于为该模块排序,下面那个ApiOperationSupport是用于为该模块下的子接口进行排序。
接口数量较多,且前端逻辑不太清晰的时候,建议搞开发的按照步骤,为每个接口进行排序,利人利己啊,要不然过个个把月,自己看接口都不知道先走哪个了。



