1.配置Swagger

Swagger的bean实例Docket;

2.Swagger配置扫描接口

Docket.select()

3.我只希望我的Swagger在生产环境中使用,在发布的时候不使用

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

4.配置API文档的分组

.groupName("Violet")

5.如何配置多个分组,多个Docket

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

总结

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

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

实体类配置

package com.mine.swagger.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 org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.PathSelectors;
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;
import java.util.Properties;


@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {

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

//    配置了Swagger的Docket的bean实例
//    enable是否启用Swagger,如果false,则不能访问
    @Bean
    public Docket docket(Environment environment){

        Profiles profiles = Profiles.of("dev","test");
//        通过environment.acceptsProfiles判断是否处在自己设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("Violet")
                .enable(flag)
                .select()
                //RequestHandlerSelectors,配置要扫描的接口的方式
//                .apis(RequestHandlerSelectors.basePackage("com.mine.swagger.controller"))
                //basePackage:指定要扫描的包
                //any(): 扫描全部
                //none(): 都不扫描
                //withClassAnnotation: 扫描类上的注解,参数是 RequestMapping.class
//                .apis(RequestHandlerSelectors.withMethodAnnotation(RestController.class))
                //withMethodAnnotation: 扫描方法上的注解  类上的注解 RestController
                .apis(RequestHandlerSelectors.basePackage("com.mine.swagger.controller"))
                //paths 过滤路径
//                .paths(PathSelectors.ant("/mine/**"))
                .build();   //
    }

//    配置Swagger信息 = apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("lv","http://120.55.89.41","616365750@qq.com");

        return new ApiInfo(
                "Violet-冰",
                "永恒花园",
                "v1.0",
                "http://120.55.89.41",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }


}
package com.mine.swagger.pojo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

//@Api(注释)
@ApiModel("用户实体类")
public class User {

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

}
package com.mine.swagger.controller;

import com.mine.swagger.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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloController {

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

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

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

    @ApiOperation("Post测试类")
    @PostMapping("/postt")
    public User postt(@ApiParam("用户名") User user){
        return user;
    }
}