网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

Swagger接口说明文档的配置和使用方法

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

目前创新互联已为近1000家的企业提供了网站建设、域名、虚拟主机网站托管运营、企业网站设计、梨树网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

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


            
                io.springfox
                springfox-swagger2
                2.7.0
            
            
                io.springfox
                springfox-swagger-ui
                2.7.0
            

其次,添加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接口说明文档的配置和使用方法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


标题名称:Swagger接口说明文档的配置和使用方法
分享URL:http://bjjierui.cn/article/ppcjcg.html

其他资讯