java配置swagger

怎么配置swagger?

导入依赖

       <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.9.2version>
        dependency>
		
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.9.2version>
        dependency>
		
        <dependency>
            <groupId>com.github.xiaoymingroupId>
            <artifactId>swagger-bootstrap-uiartifactId>
            <version>1.9.6version>
        dependency>


        <dependency>
            <groupId>com.google.guavagroupId>
            <artifactId>guavaartifactId>
            <version>20.0version>
        dependency>

编写swagger的配置文件

@Configuration
@EnableSwagger2
public class SwaggerConf {
    @Bean
    public Docket petApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //指定从那个包下面开始扫描注解
                .apis(RequestHandlerSelectors.basePackage("com.demo"))
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("api文档")
                .version("1.0.0")
                .termsOfServiceUrl("localhost:8080/swagger-ui.html")
                .description("测试")
                .build();
    }
}

访问localhost:8080/swagger-ui.html

遇到问题怎么办?

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

原因
这是因为Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher

解决方法
修改application.yaml

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

你可能感兴趣的:(java)