springboot项目集成swagger,并让shiro放行

其实写接口文档的工具有很多。比如wiki,confluence(这个用的也挺多的。安装步骤可以参见我这篇博客https://blog.csdn.net/qq_29281307/article/details/89345052

但是总结下来,还是觉得swaggerui比较好用,不仅可以在编程过程中就生成api文档,而且还可以通过swagger调试接口,这对于后台开发人员来说真是太方便了。避免再去通过postman测试的过程。

下面就直接上代码了:

首先pom 文件如下:

<!--swagger2 start-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox-swagger2.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox-swagger-ui.version}</version>
        </dependency>
        <!--swagger2 end-->

其次:编写一个SwaggerUiConfig的类来配置swagger

package com.lenovoedu.admin.config;

import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.base.Function;

import com.lenovoedu.admin.sys.constants.CommonConstants;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
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;

@Configuration
@EnableSwagger2
public class Swagger2Config {
    // 定义分隔符,配置Swagger多包
    private static final String splitor = ";";

    @Bean
    public Docket customDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(basePackage(CommonConstants.SWAGGER_SCAN_BASE_PACKAGE + splitor + "com.lenovoedu.shiro.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("YuanGL", "http://edu.lenovo.com", "yuangl@lenovoedu.com");
        return new ApiInfo("联想教育平台API接口",           //大标题
                "联想教育平台《权限系统》API接口",               //小标题
                "2.0.0",                                   //版本
                "http://edu.lenovo.com",        //服务条款URL
                contact,                                           //作者
                "联想教育",                                  //链接显示文字
                "http://edu.lenovo.com",               //网站链接
                new ArrayList<>()
        );
    }

    /**
     * 重写basePackage方法,使能够实现多包访问,复制贴上去
     * @author  teavamc
     * @date 2019/1/26
     * @param basePackage 基础包
     * @return com.google.common.base.Predicate<springfox.documentation.RequestHandler>
     */
    public Predicate<RequestHandler> basePackage(final String basePackage) {
        return input -> declaringClass(input).transform(handlerPackage(basePackage)).or(true);
    }

    private Function<Class<?>, Boolean> handlerPackage(final String basePackage)     {
        return input -> {
            // 循环判断匹配
            for (String strPackage : basePackage.split(splitor)) {
                boolean isMatch = input.getPackage().getName().startsWith(strPackage);
                if (isMatch) {
                    return true;
                }
            }
            return false;
        };
    }

    private Optional<? extends Class<?>> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }
}

至此swagger就已经配置完毕可以使用了。

如果项目中用的是shiro做权限认证的话,放行swagger需要配置以下参数:

 filterMap.put("/swagger-ui.html", "anon");
 filterMap.put("/swagger-resources/**", "anon");
 filterMap.put("/v2/**", "anon");
 filterMap.put("/webjars/**", "anon");

接下来就是swagger的使用了:在接口类和方法上加上相应的注解就可以了

@Api
@ApiOperation(value = "菜单列表分页", notes = "分页查询菜单列表接口")

@ApiImplicitParams({ @ApiImplicitParam(name = "pageInfo<SysMenu>", value = "分页对象", required = true, paramType = "body", dataType = "PageInfo<SysMenu>") })

为了避免重复造轮子,关于swagger注解的使用就请参见这位大神的博客吧:

https://blog.csdn.net/u014231523/article/details/76522486

另外推荐一篇博客:

 

SpringBoot中Swagger2多包扫描问题

https://blog.csdn.net/teavamc/article/details/86654456

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot 3集成Swagger 3的步骤如下: 1. 首先,需要在项目的pom.xml文件中添加SpringFox的依赖项。可以使用以下代码片段添加依赖项: ``` <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 这将帮助将Swagger 3集成到Spring中。 2. 在控制器类上添加@Api注解,用于定义Swagger页面的信息。例如: ``` @RestController @RequestMapping("/user") @Api(tags = "人员信息查询接口") public class UserController { // ... } ``` 这将在Swagger页面上显示"人员信息查询接口"标签。 3. 在需要生成API文档的方法上添加@ApiOperation注解,用于定义方法的说明。例如: ``` @GetMapping("/queryPage") @ApiOperation(value = "人员信息分页查询方法", notes = "查看人员信息是否返回成功") public Result queryPage(Integer pageNumber, Integer pageSize, String name) { // ... } ``` 这将在Swagger页面上显示"人员信息分页查询方法"的说明。 4. 最后,根据需要进行其他配置。例如,如果需要修改SpringBoot的路径匹配模式,可以在application.yml文件中进行配置。例如: ``` spring: mvc: pathmatch: matching-strategy: ANT_PATH_MATCHER ``` 这将使用AntPathMatcher作为路径匹配策略。 综上所述,以上是SpringBoot 3集成Swagger 3的基本步骤和配置。通过这些步骤,您可以在SpringBoot项目中使用Swagger来生成API文档。 #### 引用[.reference_title] - *1* *3* [SpringBoot整合Swagger3.0](https://blog.csdn.net/weixin_43709291/article/details/127192273)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Springboot项目集成Swagger3.0](https://blog.csdn.net/weixin_44924882/article/details/127940544)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值