2021-02-08-SpringBoot integrates Swagger

pom.xml

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

SwaggerConfig

  • In addition to the settings of the controller package, write everything you like, such as the title, description, and so on.
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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qcl.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("标题11")
                        .description("描述")
                        .version("9.0")
                        .contact(new Contact("内容名字","www.baidu.com","[email protected]"))
                        .license("asd")
                        .licenseUrl("http://www.baidu.com")
                        .build());
    }
}

Use annotations on the Controller class

  • Three annotations, used on one class and used on two methods, are all available or unnecessary annotations
  • @Api: Class description annotation, @ApiOperation: Method description annotation, @ApiImplicitParams can add this annotation when there are parameters
package com.qcl.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("demo")
@Api(tags = "类的描述dsfsdsd")
public class DemoController {

    @GetMapping("test")
    @ResponseBody
    @ApiOperation("测试的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", defaultValue = "李四"),
            @ApiImplicitParam(name = "password", value = "密码", defaultValue = "123", required = true)
    }
    )
    public String test(String username,String password) {
        return "vsdgvdfxg"+username+password;
    }
}

access

  • Run the project and visit the following link: http://localhost:8080/swagger-ui.html, you can go to the api documentation page produced by the swagger package

running result

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41270550/article/details/113748386