SpringBoot集成swagger2,及shiro配置免认证

1.引入maven依赖,本文采用2.9.2版本

maven仓库地址:https://mvnrepository.com(根据情况选择自己的版本)

<!-- swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

<!-- swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

2.编写Swagger2Config配置文件,加入@Configuration和@EnableSwagger2注解

@Configuration
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()                                // 路径根据自己项目配置
                .apis(RequestHandlerSelectors.basePackage("com.ohes.missclass.business.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("因病缺课后台api文档")
                .description("简单优雅的restful风格")
                .version("1.0")
                .build();
    }

}

3.编写TestController

@RestController
@RequestMapping("/business/school")
@Api(tags = "学校管理相关接口")
public class TestController {

    @ApiOperation(value = "接口的功能介绍",notes = "提示接口使用者注意事项",httpMethod = "GET")
    @ApiImplicitParam(dataType = "string",name = "name",value = "姓名",required = true)
    @GetMapping("/hello")
    public String hello(String name) {
        return "hello "+ name;
    }

}

4.若项目配置了shiro,则需要配置shiro免认证,不然会出现如下图情况

filterMap.put("/swagger-ui.html","anon");
filterMap.put("/swagger-resources","anon");
filterMap.put("/swagger-resources/configuration/security","anon");
filterMap.put("/swagger-resources/configuration/ui","anon");
filterMap.put("/v2/api-docs","anon");
filterMap.put("/webjars/springfox-swagger-ui/**","anon");

5.测试SpringBoot集成swagger2是否成功,访问http://172.1.5.133:9528/swagger-ui.html

server:
  port: 9528
  address: 172.1.5.133

出现以下页面说明访问成功!!!!!!!!!!!!

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Spring Boot集成Swagger的详细步骤与配置: 1. 在pom.xml文件中添加Swagger依赖 ``` <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> ``` 2. 创建Swagger配置类 创建一个SwaggerConfig类,并使用@EnableSwagger2注解开启Swagger功能。在Swagger配置类中,可以设置Swagger的一些基本信息,比如API文档的标题、描述、版本等。 ``` @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo")) .paths(PathSelectors.any()) .build(); } } ``` 3. 配置Swagger UI 在application.properties文件中添加以下配置,以开启Swagger UI: ``` #Swagger UI springfox.documentation.swagger-ui.enabled=true springfox.documentation.swagger-ui.path=/swagger-ui.html ``` 4. 配置Swagger注解 在Controller层的方法上添加Swagger注解,以便生成API文档。常用的Swagger注解有: - @Api:用于修饰Controller类,表示这个类是Swagger资源; - @ApiOperation:用于修饰Controller类中的方法,表示一个HTTP请求的操作; - @ApiParam:用于修饰方法中的参数,表示对参数的描述; - @ApiImplicitParam:用于修饰方法中的参数,表示一个请求参数的配置信息; - @ApiModel:用于修饰响应类,表示一个返回响应的信息,比如响应的数据模型; - @ApiModelProperty:用于修饰响应类中的属性,表示对属性的描述。 例如: ``` @RestController @Api(value = "用户管理", tags = "用户管理API", description = "用户管理相关接口") public class UserController { @ApiOperation(value = "获取用户列表", notes = "获取所有用户信息") @GetMapping("/users") public List<User> getUserList() { // ... } @ApiOperation(value = "获取用户信息", notes = "根据用户ID获取用户信息") @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long") @GetMapping("/users/{id}") public User getUser(@PathVariable Long id) { // ... } } ``` 5. 运行程序并访问Swagger UI 启动Spring Boot项目后,在浏览器中输入http://localhost:8080/swagger-ui.html,即可访问Swagger UI界面。在该界面中,可以查看API接口的详细信息、测试API接口等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值