使用Swagger3自动生成RESTful API文档

导入依赖

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
</dependency>

写配置类

package cn.itsp.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
	import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;


@Configuration//告诉Spring容器,这是一个配置类
@EnableWebMvc//不加上报错Failed to start bean 'documentationPluginsBootstrapper'
@EnableOpenApi//启动Swagger3功能,也可以@EnableSwagger2【非阻断式升级留下的】
public class Swagger3Config {

@Bean
public Docket createRestApi() {
    return new Docket(DocumentationType.OAS_30)
            //把写好的配置信息给进去,做一个配置
            .apiInfo(apiInfo())
            .select()
            // 配置swagger显示的controller,cn包下所有api都交给swagger代理,如果不配置则默认扫描所有后端接口
            .apis(RequestHandlerSelectors.basePackage("cn"))
            .paths(PathSelectors.any()).build();
}


//API文档页面显示信息
private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
            .title("演示Demo")
            .description("Swagger3演示项目")
            .version("1.0")
            .build();
}
}

启动项目,访问url:

swagger-ui/index.html

这里因为我配置了tomcat服务端口为80【http默认端口】
在这里插入图片描述
所以直接访问
http://localhost/swagger-ui/index.html

如果没有修改端口,请访问
http://localhost:8080/swagger-ui/index.html
在这里插入图片描述

这里可以看见之前写的RESTful规范的API

在这里插入图片描述

在Swagger UI页面可以直接测试这些接口

在这里插入图片描述

在这里插入图片描述

请求通过,可以看见响应信息

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值