Swagger接口说明文档的配置和使用方法
更新:HHH   时间:2023-1-7


这篇文章主要讲解了“Swagger接口说明文档的配置和使用方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Swagger接口说明文档的配置和使用方法”吧!

Swagger是一个开源的接口配置文档,一般用于前后端分离,代替后端人员为前端人员书写繁琐的接口文档,使后端人员从繁琐的接口文档中解脱出来。Swagger如何使用呢?首先我们要在springboot中的pom文件引入依赖包

<!-- swagger依赖组件 -->
            <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>
                <version>2.7.0</version>
            </dependency>

其次,添加swagger的配置文件,该配置文件定义了网页访问swagger2的路径以及标题,描述等信息

package com.xash.quartzDemo.config;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configurable
public class SwaggerConfig{
	   @Bean
	    public Docket api(){
	        return new Docket(DocumentationType.SWAGGER_2)
	        		.groupName("测试模块")
	                .apiInfo(getApiInfo())
	                .select()
	                .apis(RequestHandlerSelectors.basePackage("com.xash.quartzDemo.controller"))
	                .paths(PathSelectors.regex(".*/.*"))
	                .build();
	    }

	    private ApiInfo getApiInfo(){
	    	
	        return new ApiInfoBuilder()
	                .title("测试模块")
	                .description("小标题")
	                .version("1.0")
	                .build();
	    }
	    @Bean
	    public Docket api1(){
	        return new Docket(DocumentationType.SWAGGER_2)
	        		.groupName("测试模块1")
	                .apiInfo(getApiInfo1())
	                .select()
	                .apis(RequestHandlerSelectors.basePackage("com.xash.quartzDemo.controller"))
	                .paths(PathSelectors.regex(".*/.*"))
	                .build();
	    }
	    @Bean
	    private ApiInfo getApiInfo1(){
	        return new ApiInfoBuilder()
	                .title("测试模块1")
	                .description("小标题")
	                .version("1.0")
	                .build();
	    }


}

这样,我们就可以在要添加接口文档的地方利用注解添加相应的接口文档信息了

例如:

@Api(tags="这是个测试Controller")
public class PermissionController {}类上加注解,说明该类具有的功能

  @ApiOperation(value = "根据用户id查询权限")
     public String selectPermissionById(ModelMap map,@ApiParam("id") @RequestParam("id")int id){
         System.out.println("开始查询");
         map.put("name", "欢饮使用thymeleaf模板引擎");
         map.put("sysPermission", permissionService.selectPermissionById(id));
         return "index";
     }方法上加注解,表示方法的功能,以及可见在形参上加注解,指定形参,对形参进行描述

访问地址默认为192.168.2.199:8080/项目应用/swagger2-ui.html

感谢各位的阅读,以上就是“Swagger接口说明文档的配置和使用方法”的内容了,经过本文的学习后,相信大家对Swagger接口说明文档的配置和使用方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是天达云,小编将为大家推送更多相关知识点的文章,欢迎关注!

返回大数据教程...