一. build.gradle引入包

https://mvnrepository.com/artifact/io.springfox/springfox-swagger2
https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui(该包里面本身含有显示页面)

https://mvnrepository.com/artifact/com.github.xiaoymin/swagger-bootstrap-ui

二. 创建SwaggerConfig配置类

@Configuration
@EnableSwagger2
//@EnableWebMvc
public class SwaggerConfig {

    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                . (\"swagger\")
                .de ion(\"swagger API\")
                .termsOfServiceUrl(\"http://blog.csdn.net/qq_27093465?viewmode=contents\")
                .license(\"\")
                .licenseUrl(\"\")
                .version(\"1.0.0\")
                .build();
    }
}

三. WebConfig类添加静态资源访问路径

  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry){
    //swagger-ui页面显示
    registry.addResourceHandler(\"/swagger-ui.html\")
            .addResourceLocations(\"classpath:/ -INF/resources/swagger-ui.html\");
    //doc.html页面显示
    registry.addResourceHandler(\"/doc.html\")
            .addResourceLocations(\"classpath:/ -INF/resources/doc.html\");
    registry.addResourceHandler(\"/webjars/**\")
            .addResourceLocations(\"classpath:/ -INF/resources/webjars/\");
  }

目录

一. build.gradle引入包

二. 创建SwaggerConfig配置类

三. WebConfig类添加静态资源访问路径

四. controller类插入swagger注解


收藏 打印