Swagger2快速入门

170 阅读1分钟

快速入门

  • 引入依赖

    <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>
    
  • 主启动类添加注解:@EnableSwagger2

  • 添加配置

    @Configuration
    public class SwaggerConfig {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("ccb.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
        
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("测试swagger2")
                    .description("前后端接口联调文档")
                    .version("2.0.x")
                    .build();
        }
    }
    
  • 浏览器访问:http://localhost:8080/swagger-ui.html

注解说明

  • @Api(tags="测试接口"):用在Controller层
  • @ApiOperation(value="", produces = "application/json"):用在方法上
  • @ApiModel(value="类的名字",description ="类的详细描述"):用在实体类上
  • @ApiModelProperty(value="",example = "18",required = true):用在实体类属性上

相关文章