首页 > 编程语言 > 详细

springboot-配置swagger

时间:2021-08-04 22:53:04      阅读:57      评论:0      收藏:0      [点我收藏+]

1 新建springboot项目

技术分享图片

 

 

 

选择springboot

技术分享图片

 

 

 

这里根据自己选择适合的,

技术分享图片

 

 

 

继续next,这里省心的时候到了,选择自己需要的maven.

技术分享图片

 

 选的比较多的是,

技术分享图片

 

 然后next.命名自己项目名称。最后静静等待项目的启动。

2 配置swagger

 1  新建包

技术分享图片

 2

技术分享图片

 

 技术分享图片

 

 将contrller 写一点简单的逻辑。

 

2  maven 配置

<!-- 引入Swagger3依赖 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

技术分享图片

 

 

3 config 配置

package com.zhouqiang.demo.config;

/**
 * @author :zhouqiang
 * @date :2021/8/4 18:05
 * @description:
 * @version: $
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * Swagger配置类
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig {


    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo()).enable(true)
                .select()
                //apis: 添加swagger接口提取范围
                .apis(RequestHandlerSelectors.basePackage("com.zhouqiang.demo"))
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .groupName("123");
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger项目接口文档")
                .description("swagger项目描述")
                .contact(new Contact("zhoqiang", "https://www.cnblogs.com/zq1003/", ""))
                .version("1.0")
                .build();
    }
}

技术分享图片

 

 

4  加上swagger注解

技术分享图片

 

技术分享图片

 

 

3 启动项目

 有些人启动会失败。

Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could be configured.

技术分享图片

 

 

 application.properties 是没有连接数据库。

技术分享图片

 

 地址:本地+/swagger-ui/index.html

技术分享图片

 

springboot-配置swagger

原文:https://www.cnblogs.com/zq1003/p/15100381.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!