Swagger的基本使用

  1. 添加一个配置类SwaggerConfig.java

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket webApiConfig(){

        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zyx.mall.controller"))
                .paths(PathSelectors.any())
                .build();

    }

    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("接口文档")
                .description("接口文档")
                .version("1.0")
                .contact(new Contact("zhuyunxiao", "http://zyx.vip", "zhuyunxiao@qq.com"))
                .build();
    }
}

  1. 启动项目,访问url:http://localhost:8001/swagger-ui.html
  2. 常用注解
//用在controller上说明该controller的作用
@Api(description = "讲师管理")
//用在controller的方法上,用于说明方法的作用
@ApiOperation(value = "讲师列表")
//用于参数的上,参数的说明
@ApiParam(name = "id", value = "讲师ID")
//用在实体类上,对实体类的描述
@ApiModel(value="EduTeacher对象", description="讲师")
//用在实体类的字段上,对字段的描述
@ApiModelProperty(value = "讲师ID")
posted @ 2022-11-18 18:05  风一样的男子、  阅读(17)  评论(0)    收藏  举报