spring boot + swagger2 自动生成文档

2018-06-25  本文已影响0人  紫薇大舅

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
上一篇下一篇

猜你喜欢

热点阅读