springboot集成swagger

1. 引入包


    io.springfox
    springfox-swagger2
    2.9.2


    io.springfox
    springfox-swagger-ui
    2.9.2

2. 设置配置类

/**
 * * @Creator  Jacky
 * * @CreateTime 2020/6/24
 * * @Description
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swaggerShow.status}")
    private boolean swaggerStatus;

    @Bean
    public Docket createRestApi() {
        String packName = swaggerStatus?"com.dkey.core.api":"com.oo.oo";
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage(packName))
                .paths(PathSelectors.any())
                .build().apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        if(swaggerStatus) {
            return new ApiInfoBuilder()
                    .title("使用swagger查看接口")
                    .description("API 描述")
                    .termsOfServiceUrl("")
                    .version("1.0")
                    .build();
        }else {
            return new ApiInfoBuilder()
                    .title("swagger禁止使用")
                    .description("API 描述")
                    .termsOfServiceUrl("")
                    .version("")
                    .build();
        }
    }


}

3.yml设置正式和测试开关

swaggerShow:status: true

4.编写测试类

/**
 * * @Creator  Jacky
 * * @CreateTime 2020/6/24
 * * @Description
 */
@RestController
@Api(tags = "用于测试Swagger")
@RequestMapping("api/test")
public class Test {

    @PostMapping("/info")
    @ApiOperation("测试接口")
    @IgnoreAuth
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", defaultValue = ""),
            @ApiImplicitParam(name = "info", value = "用户信息", defaultValue = "", required = true)
    })
    public ReturnDto testApi(ReturnDto returnDto) {
        returnDto.setInfo("测试成功啦");
        returnDto.setInfo("我是阿猫");
        returnDto.setStatus(100);
        return returnDto;
    }
}
@ApiModel
@Data
public class ReturnDto {

    @ApiModelProperty(value = "用户信息")
    private String info;

    @ApiModelProperty(value = "用户名")
    private String username;

    @ApiModelProperty(value = "用户状态")
    private Integer status;

}

5. 输入链接http://localhost:8081/swagger-ui.html#/test

你可能感兴趣的:(springboot,IDEA,swagger)