swagger use annotations

Recent swagger development documents written notes, is actually relatively easy to use
① first introduced pom file dependencies

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

② then write configuration class

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

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger标题")
                .description("swagger描述")
                .termsOfServiceUrl("")
                .contact("这里放创作者名字")
                .version("1.0")
                .build();
    }
}

Class configuration and startup classes in the same directory level

By @Configuration annotation, let Spring to load the class configuration. Then to enable Swagger2 by @ EnableSwagger2 comment.

After createRestApi function creates Docket of Bean, apiInfo () is used to create the basic information of the Api (basic information exhibition now pages of the document). select () function returns a ApiSelectorBuilder example for controlling the interface which is exposed to Swagger to show, the present embodiment uses the specified package path to define scanning, scans all Swagger Controller API defined in this packet, and generates a document content (in addition to being @ ApiIgnore specified request)

③ increase in annotation interface controller

@Api (ddescription = "This is the description of the controller of swagger") to the annotation ---------- @RestController
@ApiOperating (value = "interface functions", notes = "Details Remarks") - --------- annotation on a method
@ApiImplicitParams ({@ApiImplicitParam (name = "request parameter name", value = "interpretation", require = true, dateType = "int"}) ----- ----- details request parameters, parameter type dateType ------------- annotation on the method, this method is annotated based on the parameters and the request parameter for the plurality of types of requests, other JavaBean method annotation
@ApiParam (value = "request of parameters and notes") before -------- annotation request parameters, such as annotations before @RequestBody

Released two original articles · won praise 0 · Views 47

Guess you like

Origin blog.csdn.net/Ayalidoo/article/details/105387659