在Spring Boot中使用Swagger

2021-10-06  本文已影响0人  jie202

导入Swagger到springboot项目中

pom.xml文件中添加

<!-- 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>

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

在config包中创建SwaggerConfig

import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
}

测试运行

http://localhost:8080/swagger-ui.html
里面包含了接口信息,model信息,swagger信息,组

配置Swagger的一些基本信息

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

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    //只要接口中返回了实体类,实体类就会被扫描到Swagger中

  //配置多个docket可以有多个分组
    @Bean
    public Docket docket1(){
    //groupName设置分组的名字
        return new Docket(DocumentationType.SWAGGER_2).groupName("sj2");
    }


    @Bean
    public Docket docket(Environment environment){

        //设置要显示swagger的环境,与application.properties相关
        Profiles profiles = Profiles.of("dev","test");
        //获取项目的环境,通过environment.acceptsProfiles判断是否处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profiles);


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //设置为false就打不开swagger-ui.html界面了
                .enable(flag)
                .groupName("sj")
                .select()
                //RequestHandlerSelectors 配置要扫描接口的方式
                //basePackage指定要扫描的包
                //any()全部
                //none()不扫描
                //withClassAnnotation扫描类上的注解,需要的参数是一个注解的反射对象   然后就会扫描有这个注解的类
//                .withMethodAnnotation()扫描方法上的注解    需要的参数是一个注解的反射对象   然后就会扫描有这个注解的方法
                .apis(RequestHandlerSelectors.basePackage("com.sj.controller"))
//                过滤什么路径,下面这行代码的意思就是纸扫描带有/sj/下的请求路径的请求
//                .paths(PathSelectors.ant("/sj/**"))
                .build();//工厂模式
    }

    //配置Swagger 信息=apiInfo
    private ApiInfo apiInfo(){
        //作者信息  姓名,可以写你的博客地址,邮件
        Contact DEFAULT_CONTACT = new Contact("j", "www.baidu.com", "xxxxx@qq.com");

        return new ApiInfo(
                "j 的swagger日记",
                "这是一个描述",
                "1.0",
                "www.baidu.com",
                DEFAULT_CONTACT, "Apache 2.0","http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
    }
}

实体类

如果在接口中返回了一个实体类,那么改实体类就会被扫描到swagger中

在发布环境中关闭swagger的代码如下

      //设置要显示swagger的环境,与application.properties相关
        Profiles profiles = Profiles.of("dev","test");
        //获取项目的环境,通过environment.acceptsProfiles判断是否处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profiles);


        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //设置为false就打不开swagger-ui.html界面了
                .enable(flag)

在实体类或方法上添加@Api注解,可以对类或者方法进行注释,然后再swagger-ui.html可以看到对应的注释。

总结

  1. swagger可以实时更新接口文档信息
  2. swagger可以测试接口返回的信息
上一篇 下一篇

猜你喜欢

热点阅读