20200213 - Swagger Introduction

OpenAPI specification (OpenAPISpeci fi cation referred OAS) is a project of the Linux Foundation, attempts to define a format used to describe the API or API defines the language, to regulate RESTful service development process, the current version is V3.0, which has been released and open source on github. (Https://github.com/OAI/OpenAPI-Speci fi cation) Swagger is the world's largest OpenAPI specification (OAS) API development tools framework that supports the development from design to testing and deployment, and documentation of the entire API lifecycle. (Https://swagger.io/)

In the following api project
Here Insert Picture Description

package com.xuecheng.api.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

//
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xuecheng"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("学成网api文档")
                .description("学成网api文档")
//                .termsOfServiceUrl("/")
                .version("1.0")
                .build();
    }

}

There is a comment above, @EnableSwagger2you will scan all have a @RestControllercontroller class

Open ports following URL
Here Insert Picture Description
points can be seen and tested
Here Insert Picture Description
but as we do not know what use the information, or what you need to represent with a comment at the time of writing code

Swagger add annotations in the Java class to generate Swagger interfaces, common Swagger annotated as follows: @Api: modify the entire class, described the role of @ApiOperation Controller: describes a method of a class or an interface @ApiParam: single Parameter Description @ApiModel: receiving an object with parameters @ApiModelProperty: upon receiving the object parameters, a description of the object field @ApiResponse: HTTP response is described wherein a @ApiResponses: HTTP response overall description @ApiIgnore: ignores the annotation using the API @ ApiError: @ApiImplicitParam error information is returned: a request parameter @ApiImplicitParams: a plurality of request parameters

Here Insert Picture Description

Published 735 original articles · won praise 42 · views 70000 +

Guess you like

Origin blog.csdn.net/qq_36344771/article/details/104299303