springboot 整合 swagger2

整合步骤

  1. pom 添加依赖
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
  1. 创建 swagger 配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    private static ApiInfo DEFAULT = null;
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2);
    }
}
  1. 浏览器访问:http://localhost:8080/swagger-ui.html(注意端口号)

在这里插入图片描述

定制配置

修改页面内容

修改 SwaggerConfig.java

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    private static ApiInfo DEFAULT = null;
    @Bean
    public Docket docket() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        ApiInfo apiInfo = new ApiInfoBuilder()
                .contact(new Contact(
                        "Swagger - k8sclient",
                        "https://github.com/vazquez/k8sclient",
                        "vazquez@gmail.com")
                )
                .title("Kubernetes Client API")
                .description("This is a simple example of a Spring Boot RESTful service.")
                .version("1.0")
                .build();
        docket.apiInfo(apiInfo);
        return docket;
    }
}

浏览器访问:http://localhost:8080/swagger-ui.html

在这里插入图片描述

去掉 basic-error-controller

方法一:指定包显示

修改 SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    private static ApiInfo DEFAULT = null;
    @Bean
    public Docket docket() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("Kubernetes Client API")
                .description("This is a simple example of a Spring Boot RESTful service.")
                .version("1.0")
                .build();
        docket.apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.vazquez.k8sclient.controller"))
                .build();
        return docket;
    }
}

方法二:指定方法显示

修改 SwaggerConfig

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    private static ApiInfo DEFAULT = null;
    @Bean
    public Docket docket() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("Kubernetes Client API")
                .description("This is a simple example of a Spring Boot RESTful service.")
                .version("1.0")
                .build();
        docket.apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build();
        return docket;
    }
}

在 controller 类中方法,即接口方法上添加 @ApiOperation 注解(只有添加了 @ApiOperation 注解的方法才显示)

@ApiOperation(value = "获取集群下的所有事件", notes = "获取集群下的所有事件")
    @GetMapping
    public Response<ApiResponse<EventsV1EventList>> listNamespacedEvent(@PathVariable String clusterName, @PathVariable String namespaceName) {
        return eventService.listNamespacedEvent(clusterName, namespaceName);
    }

去掉 Models 中的非程序内容

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快,把我桶也提着

如果对您有帮助欢迎支持哦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值