美文网首页
spring boot + swagger2 自动生成文档

spring boot + swagger2 自动生成文档

作者: 紫薇大舅 | 来源:发表于2018-06-25 10:28 被阅读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
    

    相关文章

      网友评论

          本文标题:spring boot + swagger2 自动生成文档

          本文链接:https://www.haomeiwen.com/subject/teieyftx.html