SpringBoot 整合Swagger

1.maven导入swagger包

<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>

<dependency>

<groupId>net.minidev</groupId>

<artifactId>json-smart</artifactId>

</dependency>

2.配置swagger配置类

@Configuration

@EnableSwagger2

public class Swagger2Config {

private ApiInfo apiInfo(){

ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();

apiInfoBuilder.title("Spring Boot Day 4 API 接口文档")

.version("1.0")

.description("演示授课内容使用");

return apiInfoBuilder.build();

}

@Bean

public Docket createRestApi() {

Docket docket = new Docket(DocumentationType.SWAGGER_2);

docket.apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.basePackage("com.wx.springbootday4.controller"))

//为有@Api注解的Controller生成API文档

// .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))

//为有@ApiOperation注解的方法生成API文档

// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))

.paths(PathSelectors.any())

.build().directModelSubstitute(Timestamp.class,Long.class);

return docket;

}

}

3.配置application.yml文件

spring:

mvc:

pathmatch:

matching-strategy: ant_path_matcher

4.通过http://localhost:8080/swagger-ui.html,通过地址方法访问文档

5.按照以上整合Swagger如何报

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException

可能是SpringBoot与Swagger版本不兼容,如果SpringBoot版本在2.5.6以上Swagger应该使用3.0.0的版本,SpringBoot版本在2.5.6以下Swagger可以使用2.9.2版本,上面整合的Swagger版本是2.9.2,SpringBoot是2.5.6版本

  1. SpringBoot2.7.9版本整合Swagger3.0.0版本

  1. maven导入swagger包

<dependency>

<groupId>io.springfox</groupId>

<artifactId>springfox-boot-starter</artifactId>

<version>3.0.0</version>

</dependency>

  1. 配置swagger配置类

@Configuration

public class Swagger3Config {

private ApiInfo apiInfo(){

ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();

apiInfoBuilder.title("Spring Boot Day 4 API 接口文档")

.version("1.0")

.description("演示授课内容使用");

return apiInfoBuilder.build();

}

@Bean

public Docket createRestApi() {

Docket docket = new Docket(DocumentationType.OAS_30);

docket.apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.basePackage("com.dyh"))

//为有@Api注解的Controller生成API文档

// .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))

//为有@ApiOperation注解的方法生成API文档

// .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))

.paths(PathSelectors.any());

// .build().directModelSubstitute(Timestamp.class,Long.class);

return docket;

}

}

3.配置application.yml文件

spring:

mvc:

pathmatch:

matching-strategy: ant_path_matcher

4.通过http://localhost:8080/swagger-ui/index.html,通过地址方法访问文档

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值