码迷,mamicode.com
首页 > 编程语言 > 详细

swagger整合springboot的使用

时间:2020-06-14 01:29:00      阅读:66      评论:0      收藏:0      [点我收藏+]

标签:not   str   创建   测试   引入   网络流   ase   logs   nta   

什么是swagger?

Swagger 是一个规范且完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。

Swagger 的目标是对 REST API 定义一个标准且和语言无关的接口,可以让人和计算机拥有无须访问源码、文档或网络流量监测就可以发现和理解服务的能力。当通过 Swagger 进行正确定义,用户可以理解远程服务并使用最少实现逻辑与远程服务进行交互。与为底层编程所实现的接口类似,Swagger 消除了调用服务时可能会有的猜测。

swagger整合springboot的使用:

   一、引入依赖

 

        <!--添加Swagger依赖 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>

        <!--添加Swagger-UI依赖 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

 

二、编写配置类

 1 package com.liusha.swagger.config;
 2 
 3 import org.springframework.context.annotation.Bean;
 4 import org.springframework.context.annotation.Configuration;
 5 import springfox.documentation.builders.ApiInfoBuilder;
 6 import springfox.documentation.builders.PathSelectors;
 7 import springfox.documentation.builders.RequestHandlerSelectors;
 8 import springfox.documentation.service.Contact;
 9 import springfox.documentation.spi.DocumentationType;
10 import springfox.documentation.spring.web.plugins.Docket;
11 import springfox.documentation.swagger2.annotations.EnableSwagger2;
12 
13 @Configuration  //标记为配置类
14 @EnableSwagger2  //开启Swagger在线接口文档
15 public class SwaggerConfig {
16     /**
17      * 添加摘要信息(Docket)
18      * .groupName("XXXX")配置这个Docket的组名
19      */
20     @Bean
21     public Docket docket() {
22         return new Docket(DocumentationType.SWAGGER_2).groupName("组名:")
23                 .apiInfo(new ApiInfoBuilder()
24                         .title("标题:此处是配置UI界面显示的标题信息")
25                         .description("描述:这里配置的是UI界面显示的对这个接口文档的描述信息")
26                         //new Contact()  第一个参数是创建者,第二个是连接地址(可以不配),第三个参数是邮箱(可以不配)
27                         .contact(new Contact("流-沙", "https://www.cnblogs.com/liusha-1/", "2387831285@qq.com"))
28                         .version("版本号:1.0")
29                         .build())
30                 .select()
31                 //扫描Api接口的包监听是哪一个路径的
32                 .apis(RequestHandlerSelectors.basePackage("com.liusha.swagger.controller"))
33                 .paths(PathSelectors.any())
34                 .build();
35     }
36 }

三、监听的controller类

 1 package com.liusha.swagger.controller;
 2 
 3 import io.swagger.annotations.Api;
 4 import io.swagger.annotations.ApiOperation;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 import org.springframework.web.bind.annotation.RestController;
 7 
 8 /**
 9  *  @ApiOperation()配置这个接口的描述信息
10  */
11 @RestController
12 public class TestController {
13 
14     @ApiOperation("测试SwaggerApi的接口")
15     @RequestMapping("/test")
16     public String test(){
17         return "hello swagger";
18     }
19 }

四、运行之后点击http://localhost:8080/swagger-ui.html打开SwaggerUI界面如下:

技术图片

 

 

 

 

 

 

 

swagger整合springboot的使用

标签:not   str   创建   测试   引入   网络流   ase   logs   nta   

原文地址:https://www.cnblogs.com/liusha-1/p/13122353.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!