013springboot--Swagger

目录

swagger入门

导入依赖

配置swagger--->Config

测试:http://localhost:8080/swagger-ui.html

配置Swagger

源码:

 代码:

配置扫描接口

        接口过滤

 配置是都启动Swagger

 问题: 希望Swagger 在生产环境中使用,在发布的时候不使用?

 配置Api文档分组

 问题:如何配置多个组    多个Docket实例即可

实体类配置 

user

HelloController   

 目录:

SwaggerConfig

HelloController

User

总结:


swagger入门

导入依赖

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

配置swagger--->Config

@Configuration
@EnableSwagger2     //开启Swagger2的自动装配
public class SwaggerConfig {

}

测试:http://localhost:8080/swagger-ui.html

配置Swagger

Swagger的bean实例Docket

源码

    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    //配置Swagger2的apiInfo信息
    private ApiInfo apiInfo() {
        //作者信息
        Contact contact = new Contact("gh", "https://blog.csdn.net/gh_xiaohe?spm=1000.2115.3001.5343", "2495140780@qq.com");
        return new ApiInfo(
                "Swagger2 API文档",    //标题
                "文档描述写啥我也不知道!!!",       //描述
                "V1.0",                             //版本信息
                "localhost:8080/hello",     //组织链接
                contact,                                    //联系人信息
                "Apache 2.0",                       //许可
                "http://www.apache.org/licenses/LICENSE-2.0",   //许可连接
                new ArrayList()                //扩展
        );
    }

 代码:

    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }
    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }

    //配置Swagger2的apiInfo信息
    private ApiInfo apiInfo() {
        //作者信息
        Contact contact = new Contact(
                "gh",
                "https://blog.csdn.net/gh_xiaohe",
                "2495140780@qq.com");
        return new ApiInfo(
                "Swagger2 API文档",    //标题
                "文档描述写啥我也不知道!!!",       //描述
                "V1.0",                             //版本信息
                "https://www.bilibili.com/",    //组织链接
                contact,                                    //联系人信息
                "Apache 2.0",                       //许可
                "http://www.apache.org/licenses/LICENSE-2.0", //许可连接
                new ArrayList()                //扩展
        );
    }

配置扫描接口

        接口过滤

Docket.select()

    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()

                
                .build();

    }
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()

                 /*
               只有两个选项 .apis()  .paths
                    拉姆达表达式  是个接口  使用实现类
                    RequestHandlerSelectors  配置要扫描接口的方式
                       basePackage:根据包扫描接口
                       any():扫描所有 项目中所有的接口都会被扫描
                       none():都不扫描
                       withClassAnnotation():根据类上的注解进行扫描
                       withMethodAnnotation():根据方法上的注解进行扫描
              */
                .apis(RequestHandlerSelectors.basePackage("com.gh.controller"))


                .build();


    }

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()

                 /*
               只有两个选项 .apis()  .paths
                    拉姆达表达式  是个接口  使用实现类
                    RequestHandlerSelectors  配置要扫描接口的方式
                       basePackage:根据包扫描接口
                       any():扫描所有 项目中所有的接口都会被扫描
                       none():都不扫描
                       withClassAnnotation():根据类上的注解进行扫描    传入 注解的反射对象
                       withMethodAnnotation():根据方法上的注解进行扫描  withClassAnnotation(GetMapping.class))
              */
                .apis(RequestHandlerSelectors.basePackage("com.gh.controller"))
                //过滤  过滤什么路径
                .paths(PathSelectors.ant("/gh/**"))  //扫描这个包,并且 只扫描带有 gh 这个请求的接口

                .build();


    }

 配置是都启动Swagger

//enable 是否启动 Swagger 如果为false 则 Swagger 不能在浏览器访问
.enable(false)  //默认true
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //enable 是否启动 Swagger 如果为false 则 Swagger 不能在浏览器访问
                .enable(false) //默认true
                .select()

                 /*
               只有两个选项 .apis()  .paths
                    拉姆达表达式  是个接口  使用实现类
                    RequestHandlerSelectors  配置要扫描接口的方式
                       basePackage:根据包扫描接口
                       any():扫描所有 项目中所有的接口都会被扫描
                       none():都不扫描
                       withClassAnnotation():根据类上的注解进行扫描    传入 注解的反射对象
                       withMethodAnnotation():根据方法上的注解进行扫描  withClassAnnotation(GetMapping.class))
              */
                .apis(RequestHandlerSelectors.basePackage("com.gh.controller"))
                //过滤  过滤什么路径
//                .paths(PathSelectors.ant("/gh/**"))  //扫描这个包,并且 只扫描带有 gh 这个请求的接口
                .build();


    }

 

 问题: 希望Swagger 在生产环境中使用,在发布的时候不使用?

  • 判断是不是生产环境  flag = false
  • 注入enable (flag)

application.properties

        spring.profiles.active=dev

application-dev.properties

       server.port=8081

application-pro.properties

        server.port=8082

    @Bean
    public Docket docket(Environment environment) {

        //设置要显示的Swagger环境
        Profiles profiles = Profiles.of("dev","test");

        //获取项目的环境
        //通过environment.acceptsProfiles 来判断是否处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //enable 是否启动 Swagger 如果为false 则 Swagger 不能在浏览器访问
                .enable(flag)  //默认true
                .select()

                 /*
               只有两个选项 .apis()  .paths
                    拉姆达表达式  是个接口  使用实现类
                    RequestHandlerSelectors  配置要扫描接口的方式
                       basePackage:根据包扫描接口
                       any():扫描所有 项目中所有的接口都会被扫描
                       none():都不扫描
                       withClassAnnotation():根据类上的注解进行扫描    传入 注解的反射对象
                       withMethodAnnotation():根据方法上的注解进行扫描  withClassAnnotation(GetMapping.class))
              */
                .apis(RequestHandlerSelectors.basePackage("com.gh.controller"))
                //过滤  过滤什么路径
//                .paths(PathSelectors.ant("/gh/**"))  //扫描这个包,并且 只扫描带有 gh 这个请求的接口
                .build();


    }

 

 配置Api文档分组

.groupName("Hello-Swagger")

 

 问题:如何配置多个组    多个Docket实例即可

    /**
     *  配置多个分组 只需往往容器里多注入几个Docket的bean
     */

    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("AAA");
    }

    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("BBB");
    }

    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("CCC");
    }

 

实体类配置 

@ApiModel

@ApiModelProperty

@Api(tags = "用户控制")

@ApiOperation("Hello控制类")    //Operation 接口  不是放在类上,是方法

@ApiParam 方法形参 

 

 

 

user

//给生成的文档加一个注释
@Api(注释)
@ApiModel("User:用户实体类")
public class User {

    @ApiModelProperty("用户名")
    public String username;

    @ApiModelProperty("密码")
    public String password;
}

HelloController   

                只和 返回值 中  有无实体类有关 和有无注解无关

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello(){
        return "Hello,Swagger";
    }

    //只要我们的接口中,返回值中存在实体类,他就会被扫描到 Swagger 中
    @GetMapping("/user")
    public User user1(){
        return new User();
    }
}

    //Operation 接口 不是放在类上,是方法
    @ApiOperation("Hello控制类")
    @PostMapping("/hello2")
    public String hello2(@ApiParam("用户名") String name){
        return name;
    }

 

 目录:

SwaggerConfig

package com.gh.config;

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 org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.builders.PathSelectors;
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;

@Configuration
@EnableSwagger2     //开启Swagger2的自动装配
public class SwaggerConfig {



    @Bean
    public Docket docket(Environment environment) {

        //设置要显示的Swagger环境
        Profiles profiles = Profiles.of("dev","test");

        //获取项目的环境
        //通过environment.acceptsProfiles 来判断是否处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("Hello-Swagger")
                //enable 是否启动 Swagger 如果为false 则 Swagger 不能在浏览器访问
                .enable(flag)  //默认true
                .select()

                 /*
               只有两个选项 .apis()  .paths
                    拉姆达表达式  是个接口  使用实现类
                    RequestHandlerSelectors  配置要扫描接口的方式
                       basePackage:根据包扫描接口
                       any():扫描所有 项目中所有的接口都会被扫描
                       none():都不扫描
                       withClassAnnotation():根据类上的注解进行扫描    传入 注解的反射对象
                       withMethodAnnotation():根据方法上的注解进行扫描  withClassAnnotation(GetMapping.class))
              */
                .apis(RequestHandlerSelectors.basePackage("com.gh.controller"))
                //过滤  过滤什么路径
//                .paths(PathSelectors.ant("/gh/**"))  //扫描这个包,并且 只扫描带有 gh 这个请求的接口
                .build();

    }

    

    //配置Swagger2的apiInfo信息
    private ApiInfo apiInfo() {
        //作者信息
        Contact contact = new Contact(
                "gh",
                "https://blog.csdn.net/gh_xiaohe",
                "2495140780@qq.com");
        return new ApiInfo(
                "Swagger2 API文档",    //标题
                "文档描述写啥我也不知道!!!",       //描述
                "V1.0",                             //版本信息
                "https://www.bilibili.com/",    //组织链接
                contact,                                    //联系人信息
                "Apache 2.0",                       //许可
                "http://www.apache.org/licenses/LICENSE-2.0", //许可连接
                new ArrayList()                //扩展
        );
    }

//    private ApiInfo apiInfo() {
//        return new ApiInfo(
//               "Api Documentation",
//                "Api Documentation",
//                "1.0",
//                "urn:tos",
//                DEFAULT_CONTACT,
//                "Apache 2.0",
//                "http://www.apache.org/licenses/LICENSE-2.0",
//                new ArrayList());
//    }


    /**
     *  配置多个分组 只需往往容器里多注入几个Docket的bean
     */

    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("AAA");
    }

    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("BBB");
    }

    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("CCC");
    }

}

HelloController

package com.gh.controller;

import com.gh.pojo.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello(){
        return "Hello,Swagger";
    }

    //只要我们的接口中,返回值中存在实体类,他就会被扫描到 Swagger 中
    @GetMapping("/user")
    public User user(){
        return new User();
    }

    //Operation 接口 不是放在类上,是方法
    @ApiOperation("Hello控制类")
    @GetMapping("/hello2")
    public String hello2(@ApiParam("用户名") String name){
        return name;
    }

    @ApiOperation("post")
    @PostMapping("/post")
    public User post(@ApiParam("用户名") User user){
        return user;
    }
}

User

//给生成的文档加一个注释
//@Api(注释)
@ApiModel("User:用户实体类")
public class User {

    @ApiModelProperty("用户名")
    public String username;

    @ApiModelProperty("密码")
    public String password;
}

总结:

  1. 我们可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息
  2. 接口文档实时更新
  3. 可以在线测试

【注意点】在正式发布的时候,关闭Swagger!!!出于安全考虑。而且节省运行的内存;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gh-xiaohe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值