Springboot + Swagger2.0

本文介绍如何在SpringBoot项目中整合Swagger,包括依赖引入、基本配置及自定义操作等内容,帮助开发者快速上手并实现API文档自动生成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Springboot 整合Swagger

1.1引入依赖

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

1.2 启用swagger

在启动类中加入@EnableSwagger2

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oyOpDUIw-1614862456679)(https://secure-static.wolai.com/static/9TdMnLjRp8ZyiiwMQcXyzR/image.png)]

1.3 常用注释

注释作用使用
@ApiModelProperty用在出入参数对象的字段上对象属性描述
@Api用户controller上协议集描述
@ApiOperation用于controller的方法上方法的描述
@ApiModel用在返回对象类上描述返回对象的意义

1.4 启动后访问

http://localhost:11111/swagger-ui.html#/

在这里插入图片描述

2. 自定义操作

在权限管理中每新增一个接口意味着新增了权限,为了不手动去添加权限,能不能再启动项目时自动扫描权限并写入数据库权限表呢?

可以写个监听事件,通过获取swagger的信息,将接口信息全部保存到数据库。

@Component
public class InitSwaggerInfo implements ApplicationListener<ApplicationStartedEvent> {
    @Autowired
    private DocumentationCache documentationCache;
    @Autowired
    private ServiceModelToSwagger2Mapper mapper;
    @Override
    public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
        Documentation documentation = documentationCache.documentationByGroup("default");
        if (documentation == null) {

        }
        Swagger swagger = mapper.mapDocumentation(documentation);

    }
}

通过 documentationCache 以及 ServiceModelToSwagger2Mapper 可以获取到所有的扫描到的信息,你可以找到你要的信息

当然swagger可能不会完全适配你的需求,比如:除了url,方法名这些信息以外我还有自定义的注解去设置这个接口是属于什么层级,是否登录可访问等等

这时候我们只需要自定义一个注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Permission {

    /**
     * 公有接口,不做权限校验
     * @return
     */
    boolean isPublic() default false;

    /**
     * 登录可访问
     * @return
     */
    boolean isLogin() default false;
} 

使用注解
在这里插入图片描述

实现 OperationBuilderPlugin接口

public class CustomPlugin implements OperationBuilderPlugin {
    private final ObjectMapper mapper = new ObjectMapper();
    @Override
    public void apply(OperationContext operationContext) {
        PermissionData permissionData = new PermissionData();
        permissionData.setUrl(operationContext.requestMappingPattern());
        permissionData.setMethod(operationContext.httpMethod().name());
        permissionData.setAction(operationContext.getName());
        Optional<Permission> annotation = operationContext.findAnnotation(Permission.class);
        if(annotation.isPresent()){
            Permission permission = annotation.get();
            permissionData.setLogin(permission.isLogin());
            permissionData.setPublic(permission.isPublic());
        }
        try {
            operationContext.operationBuilder().notes(mapper.writeValueAsString(permissionData));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean supports(DocumentationType documentationType) {
        return true;
    }
}
@Configuration
public class SwaggerConfig {
   @Bean
   public CustomPlugin getCustomPlugin(){
       return new CustomPlugin();
   }

}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值