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,通过地址方法访问文档


版权声明:本文为weixin_45834289原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_45834289/article/details/129665921