SpringBoot使用Swagger2创建API文档

SpringBoot使用Swagger2创建API文档

一、在pom文件中加入以下依赖

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

二、在启动类同一目录下创建Swagger2.java

内容如下:

@Configuration
@EnableSwagger2
public class Swagger2 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxxx.controller"))
                .paths(PathSelectors.any())
                .build();
    }
​
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot入门利用swagger构建api文档")
                .description("使用restfun风格")
                .termsOfServiceUrl("http://www.xxx.cn")
                .version("1.0")
                .build();
    }
}

三、配置API内容

1、在想要创建API的类上加上

@Api(description = "User Controller")
也就是这个类的描述

2、在方法上使用

@ApiOperation(value = "不需要参数返回所有user数据", notes = "不需要参数返回list", httpMethod = "GET")

四、访问API页面

SpringBoot应用成功启动后,访问
    http://localhost:8080/swagger-ui.html
就可以看到相应的Controller以及里面的方法。可以点击对应的Try it out 来进行服务的方法,如果有参数的话,可以在Parameters 里面填写参数

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值