springboot使用springfox-boot-starter创建swagger接口文档

官网说明

http://springfox.github.io/springfox/docs/current

添加maven依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <version>2.3.5.RELEASEversion>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-boot-starterartifactId>
            <version>3.0.0version>
        dependency>
    dependencies>

示例Controller

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(value = "myApi", tags = "API测试接口")
public class MyController {

    @ApiOperation("测试接口1")
    @GetMapping("test1")
    public String test1(@ApiParam("测试参数1") String test){
        return "test1";
    }

    @ApiOperation("测试接口2")
    @GetMapping("test2")
    public String test2(@ApiParam("测试参数2") String test){
        return "test2";
    }
}

配置Swagger文档

简单配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(ApiInfo.DEFAULT);
    }
}

API地址:http://localhost:8080/swagger-ui/index.htmlspringboot使用springfox-boot-starter创建swagger接口文档_第1张图片

全面配置

参照:http://springfox.github.io/springfox/docs/current/#quick-start-guides

你可能感兴趣的:(Spring,Swagger,spring,boot,java,spring)