VIP免费

springboot整合swagger-bootsarp-ui实现接口文档自动生成

精品 原创 swagger接口文档
32
DEMO程序园
程序猿 2021-04-17
积分:0

简介:

最近开源项目中在集成swagger2,集成好之后发现UI效果很差,于是各种百度,发现了swagger-bootstrap-ui这个UI,于是开始了一波复制粘贴的操作,先来看以下效果: 之前的效果: 一、添加依赖<dependency>   <groupId>io.springfox</groupId>   

最近开源项目中在集成swagger2,集成好之后发现UI效果很差,于是各种百度,发现了swagger-bootstrap-ui这个UI,于是开始了一波复制粘贴的操作,先来看以下效果:

image-20210417100616350

之前的效果:

image-20210417100949411

一、添加依赖

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.9.2</version>
</dependency>
<dependency>
   <groupId>com.github.xiaoymin</groupId>
   <artifactId>swagger-bootstrap-ui</artifactId>
   <version>1.9.6</version>
</dependency>

二 、新增swagger2配置文件

package com.democxy.common.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
* swagger配置类
* @author shiling_deng
* @version 2021/04/15
*/
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {

   @Bean
   public Docket createRestApi() {
       return new Docket(DocumentationType.SWAGGER_2)
               .apiInfo(apiInfo())
               .select()
               // 根据包名扫描接口,容易暴露不想公开的接口
               // .apis(RequestHandlerSelectors.basePackage("com.democxy"))
               // 根据注解扫描,建议使用这种方式,灵活性更高
               .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
               .paths(PathSelectors.any())
               .build();
   }
   private ApiInfo apiInfo() {
       return new ApiInfo(
               "EASYBOOT-ADMIN接口文档",
               "springboot简易开发平台" ,
               "V1.0.0",
               "https://gitee.com/shuangerduo",new Contact("shiling_deng", "", ""),
               "",
               "https://gitee.com/shuangerduo",
               new ArrayList<>()
       );
   }
}

三、启动项目

启动项目,不报错,然后访问地址: http://ip:port/doc.html 即可

如:http://localhost:8080/doc.html


注意事项

如果想要api文档在未登录的情况下查看,记得放行 /swagger-resources/** 路径以及处理webjars静态资源


Swagger常用注解

@ApiOperation

使用于在方法上,表示一个http请求的操作 源码中属性太多,记几个比较常用 value用于方法描述 notes用于提示内容 tags可以重新分组(视情况而用)

@ApiParam

使用在方法上或者参数上,字段说明;表示对参数的添加元数据(说明或是否必填等) name–参数名 value–参数说明 required–是否必填

@ApiModel()

使用在类上,表示对类进行说明,用于参数用实体类接收 value–表示对象名 description–描述

@ApiModelProperty()

使用在方法,字段上,表示对model属性的说明或者数据操作更改 value–字段说明 name–重写属性名字 dataType–重写属性类型 required–是否必填 example–举例说明 hidden–隐藏



评论
最新发布
2025-10-22
2025-10-22
2025-07-22
2025-06-20
2025-05-27
2025-05-21
2025-05-18
2025-05-15
2025-05-15
2025-05-14
layui

微信扫码关注DEMO程序园公众号

本周热门
1067
1003
953
726
721
720
686
572
396
387
热门下载
27
20
19
14
14
12
12
12
12
11