spring boot+ swagger

本文详细介绍如何在SpringBoot项目中集成Swagger2,包括依赖添加、注解使用、配置类编写及API文档生成方法,帮助开发者快速上手,实现RESTful API的自动生成与测试。

spring boot+ swagger

依赖

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

启用注解

@EnableSwagger2

配置

@Configuration
public class SwaggerConfig {

@Bean
public Docket swaggerSpringMvcPlugin() {
    return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();
}
}

Controller注解

注意要加 RequestBody 或 RequestParam等注解

 @ApiOperation(value = "test", httpMethod = "POST")
    @PostMapping("t6")
    public void t6(@RequestBody User user) {

    }

配合@value注解可以控制在各种profile下是否开启,注意enable方法

  @Bean
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(new ApiInfo("","","","","","",""))
                .enable(true);
    }

地址

http://localhost:8081/swagger-ui.html

json生成地址

http://localhost:8081/v2/api-docs

json可视化地址

http://editor.swagger.io/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值