spring boot + swagger2 自动生成文档

spring boot + swagger2 自动生成文档

一、配置build gradle

compile('io.springfox:springfox-swagger2:2.7.0')
compile('io.springfox:springfox-swagger-ui:2.7.0')

二、配置文件

@Configuration
@EnableSwagger2
public class Swagger2 {

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

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxx")
                .description("xxx")
                .termsOfServiceUrl("xxx")
                .contact("ziweidajiu")
                .version("1.0")
                .build();
    }
}

三、基本使用

  • 类上方添加
@Api(value = "UserController", description = "用户相关接口")
  • 方法上方添加
@ApiOperation(value="注册用户", notes="参数列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tuoyouUser", value = "用户信息", required = true ,dataType = "TuoyouUser"),
            @ApiImplicitParam(paramType = "header", name = "Authorization", value = "token", dataType = "String", required = true, defaultValue = "123")
    })

四、使用Security注意事项

    .antMatchers("/css/**", "/js/**","/images/**", "/webjars/**", "**/favicon.ico", "/index").permitAll()

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers(
                        "/swagger-ui.html",
                        "/v2/api-docs",
                        "/swagger-resources",
                        "/swagger-resources/configuration/ui",
                        "/swagger-resources/configuration/security"
                );
    }

五、请求网址

 http://localhost:8080/swagger-ui.html
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容