Swagger初步学习

一,Swagger简介

  • 号称世界上最流行的API框架
  • Restful Api 文档在线自动生成器 => API 文档 与API 定义同步更新
  • 直接运行,在线测试API接口(其实就是controller requsetmapping)
  • 支持多种语言 (如:Java,PHP等)
    官网:https://swagger.io/

二,Springboot集成Swagger

要求:jdk 1.8 + 否则swagger2无法运行
添加Maven依赖

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.9.2</version>
</dependency>

编写HelloController,测试确保运行成功!

@RestController
public class HelloController {

    @RequestMapping(value = "/hello")
    public String hello() {
        return "hello";
    }
}

MySwagger 配置类

点击查看代码
package com.example.demo.utils;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT;

/**
 * 使用swagger需要创建一个配置类,并开启swagger配置
 * ps:swagger和 swagger2 不能通用
 *
 * 步骤:
 * 1.将swaggerConfig注册到ioc容器中
 * 2.开启swagger2功能
 * 3.运行项目,访问swagger-ui.html
 */
@Configuration // 就是@Component
@EnableSwagger2
public class MySwagger {

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()// 通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .build();
    }
    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group1");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group2");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group3");
    }



    @Bean
    public ApiInfo apiInfo(){
        ApiInfo apiInfo = new ApiInfo(
                "sky的swagger文档" //标题
                , "天空蓝蓝的" // 描述
                , "v1.0" // 版本信息
                , "https://home.cnblogs.com/u/youngchen/" // 一个url地址
                , DEFAULT_CONTACT// 作者信息对象
                , "Apache 2.0" // 开源版本号
                , "http://www.apache.org/licenses/LICENSE-2.0" // 开源版本号地址
                , new ArrayList<VendorExtension>());
        return apiInfo;
    }
}


测试运行:访问:http://localhost:8080/swagger-ui.html

常用注解

posted @ 2022-06-08 15:08  杨杨杨0411  阅读(38)  评论(0)    收藏  举报