springfox-swagger2

springfox-swagger2

文档:http://springfox.github.io/springfox/docs/current/#customizing-the-swagger-endpoints
不说别的, 2.9.0的UI特别帅,所以还是用2.9.0的版本

maven

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.0</version>
</dependency>

gradle

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

java

/**
     * group :rest
     * 扫描注解了@ApiOperation的方法生成API接口文档
     * @return
     */
    @Bean
    public Docket RestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("rest")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * group :app
     * 扫描controller包生成API接口文档
     * @return
     */
    @Bean
    public Docket packageApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("app")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("tech.yiyehu.modules.app.controller"))  
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("帮帮校园")
                .description("帮帮校园")
                .termsOfServiceUrl("http://www.yiyehu.tech/")
                .version("1.0")
                .build();
    }

这里写图片描述

swagger2常用注解

常用注解:
@Api()用于类,表示标识这个类是swagger的资源
例:

@Api(value="用户登录",tags={"用户登录接口"})

@ApiOperation()用于方法,表示一个http请求的操作
例:

@ApiOperation(value="", httpMethod = "", response = "接口返回参数类型", notes = "接口发布说明")

@ApiParam()用于方法,参数,字段说明,表示对参数的添加元数据(说明或是否必填等)


@ApiModel()用于类,表示对类进行说明,用于参数用实体类接收


@ApiModelProperty()用于方法,字段 ,表示对model属性的说明或者数据操作更改


@ApiIgnore()用于类,方法,方法参数 ,表示这个方法或者类被忽略


@ApiImplicitParam() 用于方法 ,表示单独的请求参数


@ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam

扫描二维码关注公众号,回复: 2039509 查看本文章

@RequestHeader(defaultValue=”${param1.defaultValue}”) 设置默认请求头

猜你喜欢

转载自blog.csdn.net/canyanruxue/article/details/80500333