spring boot 配置Swagger2生成在线接口文档步骤

1:添加swagger2依赖

<!--swagger-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <scope>provided </scope>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <scope>provided </scope>
</dependency>

2:在@configruation中配置swaggwer2,其中@swagger2注解必须在类上

注意:在多工程里面配置类想要被加载,需要依赖此工程后,在启动类加上@ComponentScan(basePackages = {"com.qiyun 包路径"},要不然不会加载到别的工程的配置类)

@Configuration
@EnableSwagger2 //swagger2注解
public class SwaggerConfig {
    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }

    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("网站-课程中心API文档")
                .description("本文档描述了课程中心微服务接口定义")
                .version("1.0")
                .contact(new Contact("java", "http://qiyun.com", "[email protected]"))
                .build();
    }
}

3:在类、方法、参数上加上此注解

定义在类上:@Api(tags="")  这里面注意要用tags 

定义在方法上:@ApiOperation

定义在参数上:@ApiParam

定义在属性上: @ApiModelProperty

@Api("讲师管理")
@RestController
@RequestMapping("/edusevice/teacher")
public class EduTeacherController {
   
   
// @PathVariable 是得到路径上的参数
@ApiOperation("根据id逻辑删除讲师信息")
@DeleteMapping("deleteById/{ids}")
public boolean deleteById(
        @ApiParam(name="ids" ,value = "讲师id",required = true)
        @PathVariable String ids){
    boolean b = eduTeacherService.removeById(ids);
    return b;
}

4:访问地址:http://localhost:8001/swagger-ui.html,一定要记住后缀为 /swagger-ui.html 这个是写死的

扫描二维码关注公众号,回复: 12418787 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_39564710/article/details/113154958