2.配置Swaggger

Swagger实例Bean是Docket,所以通过配置Docket实例来配置Swaggger

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

通过apiInfo()属性配置文档信息

private ApiInfo apiInfo(){
        Contact contact = new Contact("张三","https://www.baidu.com/","123456789@qq.com");
        return new ApiInfo(
                "张三的SwaggerApi文档",
                "学习如何配置Swagger",
                "v1.0",
                "https://www.baidu.com/",
                contact,
                "apache 2.0许可",
                "https://httpd.apache.org/",
                new ArrayList<>()
        );
    }

Docket 实例关联 apiInfo()

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

通过select()方法配置扫描接口的方式

	@Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
            	.apiInfo(apiInfo())
                .select()               
            	.apis(RequestHandlerSelectors.basePackage("com.dream.springboot04.controller"))
                .build();
    }
any() // 扫描所有,项目中的所有接口都会被扫描到
none() // 不扫描接口
// 通过方法上的注解扫描,如withMethodAnnotation(GetMapping.class)只扫描get请求 
withMethodAnnotation(final Class<? extends Annotation> annotation) 
// 通过类上的注解扫描,如.withClassAnnotation(Controller.class)只扫描有 controller注解的类中的接口 
withClassAnnotation(final Class<? extends Annotation> annotation) 
basePackage(final String basePackage) // 根据包路径扫描接口

通过path配置接口扫描过滤

	@Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
            	.apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dream.springboot04.controller"))
                .paths(PathSelectors.any())
                .build();
    }
any() // 任何请求都扫描
none() // 任何请求都不扫描 
regex(final String pathRegex) // 通过正则表达式控制 
ant(final String antPattern) // 通过ant()控制

通过enable()方法配置是否启用swagger

	@Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
            	.apiInfo(apiInfo())
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dream.springboot04.controller"))
                .paths(PathSelectors.any())
                .build();
    }

通过项目环境动态配置是否启用swagger

	@Bean
    public Docket docket(Environment environment){
        Profiles of = Profiles.of("dev");
        boolean b = environment.acceptsProfiles(of);
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .enable(b)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dream.springboot04.controller"))
                .paths(PathSelectors.any())
                .build();
    }

通过groupName()方法配置API分组

	@Bean
    public Docket docket(Environment environment){
        Profiles of = Profiles.of("dev");
        boolean b = environment.acceptsProfiles(of);
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .groupName("张三")
                .enable(b)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.dream.springboot04.controller"))
                .paths(PathSelectors.any())
                .build();
    }

若想配置多个分组只需要配置多个docket即可

实体配置

新建一个实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("用户实体")
public class User {
    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}

只要这个实体在请求接口的返回值上(即使是泛型),都能映射到实体项中

	@GetMapping("/getUser")
    public User getUser(@ApiParam("传入的用户名") String name,@ApiParam("传入的密码") String password){
        return new User(name,password);
    }

常用注解

Swagger注解简单说明
@Api(tags = “xxx模块说明”)作用在模块类上
@ApiOperation(“xxx接口说明”)作用在接口方法上
@ApiModel(“xxxPOJO说明”)作用在模型类上:如VO、BO
@ApiModelProperty(value = “xxx属性说明”,hidden = true)作用在类方法和属性上,hidden设置为true可以隐藏该属性
@ApiParam(“xxx参数说明”)作用在参数、方法和字段上,类似@ApiModelProperty