Jboot整合Swagger

    本文演示Jboot整合Swagger。
一、项目搭建
    新建一个Maven项目,引入依赖:   

<properties>
        <jBoot.version>3.10.0</jBoot.version>
        <swagger.version>1.5.21</swagger.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.jboot</groupId>
            <artifactId>jboot</artifactId>
            <version>${jBoot.version}</version>
        </dependency>

        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-core</artifactId>
            <version>${swagger.version}</version>
        </dependency>
    </dependencies>

二、添加Swagger页面资源
        由于 Swagger 运行的时候,需要 html 页面来显示,因此还需要去 Swagger 的官方网站把对应的 html 资源下载下来,并放入 webapp/swaggerui 目录下。
        swagger ui 的下载地址是:https://github.com/swagger-api/swagger-ui ,下载其 dist 目录即可,只需要这个目录里的文件。
        请注意,webapp和src是同级目录。
三、编写测试启动类

@RequestMapping("/swagger")
@Api(description = "用户相关接口文档", basePath = "/swagger", tags = "abc")
public class MySwaggerController extends JbootController {

    @ApiOperation(value = "用户列表", httpMethod = "GET", notes = "user list")
    public void index() {
        renderJson(Ret.ok("k1", "v1").set("name", getPara("name")));
    }
    @ApiOperation(value = "添加用户", httpMethod = "POST", notes = "add user")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", paramType = ParamType.FORM, dataType = "string", required = true),
            @ApiImplicitParam(name = "k1", value = "k1", paramType = ParamType.FORM, dataType = "string", required = true)
    })
    public void add(String username) {
        renderJson(Ret.ok("k1", "v1").set("username", username));
    }
    public static void main(String[] args) {
        JbootApplication.setBootArg("jboot.swagger.path", "/swaggerui");
        JbootApplication.setBootArg("jboot.swagger.title", "Jboot API 测试");
        JbootApplication.setBootArg("jboot.swagger.description", "这是一个Jboot对Swagger支持的测试demo。");
        JbootApplication.setBootArg("jboot.swagger.version", "1.0");
        JbootApplication.setBootArg("jboot.swagger.termsOfService", "http://jboot.io");
        JbootApplication.setBootArg("jboot.swagger.host", "127.0.0.1:8080");
        JbootApplication.run(args);
    }
}

四、启动及效果:

启动main函数并访问http://localhost:8080/swaggerui/

测试验证OK。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Security整合Swagger的过程中,我们需要进行以下几个步骤: 1. 配置Spring Security:在Spring Security配置类中,我们可以使用`WebSecurityConfigurerAdapter`来配置权限过滤和访问控制。可以在`configure(HttpSecurity http)`方法中添加`.antMatchers("/swagger-ui.html").permitAll()`来允许Swagger UI页面的访问。这样,Swagger UI页面将不会被Spring Security拦截。 2. 配置静态资源:Swagger UI页面需要访问一些静态资源,例如Swagger API文档和UI配置文件。我们可以在Spring Security配置类中使用`WebSecurity.configure(WebSecurity web)`方法来配置这些静态资源的访问权限。可以使用`web.ignoring().antMatchers("/v2/api-docs", "/swagger-resources/configuration/ui", "/swagger-resources", "/swagger-resources/configuration/security", "/swagger-ui.html")`来允许这些静态资源的访问。 3. 添加相关依赖:在项目的pom.xml文件中,我们需要添加Spring Security和Swagger的相关依赖。可以添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> ``` 这些依赖将帮助我们实现Spring Security和Swagger整合。 综上所述,以上是实现Spring Security整合Swagger的方法。通过配置Spring Security和静态资源,以及添加相关依赖,我们可以实现在Spring Boot项目中使用Spring Security保护接口并允许Swagger UI的访问[1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值