swagger和springboot整合出现问题Failed to execute goal org.apache.maven.plugins

概述

错误为:
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project demo: There are test failures.

今天整合springboot和swagger
结果出现个错误:

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.2:test (default-test) on project demo: There are test failures.
界面为:
在这里插入图片描述
看日志:
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.246 s <<< FAILURE! - in com.example.demo.DemoApplicationTests
contextLoads Time elapsed: 0 s <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.context.ApplicationContextException: Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerException
Caused by: java.lang.NullPointerException

分析原因:
应该是swagger3和springboot的默认配置冲突(我猜的)
先说解决方法:
3步骤:
1.改pom
2.写配置类
3.主类开启@Enablespringmvc
第一步:

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

第二步:
配置类:

package com.example.demo.config;



import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@EnableWebMvc

public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)  // DocumentationType.SWAGGER_2 固定的,代表swagger2
                //.groupName("分布式任务系统") // 如果配置多个文档的时候,那么需要配置groupName来分组标识
                .apiInfo(apiInfo()) // 用于生成API信息
                .select() // select()函数返回一个ApiSelectorBuilder实例,用来控制接口被swagger做成文档
                // 扫描指定包下的接口,最为常用
                .apis(RequestHandlerSelectors.basePackage("com"))
                //.withClassAnnotation(RestController.class) // 扫描带有指定注解的类下所有接口
                //.withMethodAnnotation(PostMapping.class) // 扫描带有只当注解的方法接口
                //.apis(RequestHandlerSelectors.any()) // 扫描所有

                // 选择所有的API,如果你想只为部分API生成文档,可以配置这里
                .paths(PathSelectors.any()
                                // 满足条件的路径,该断言总为true
                        //.none() // 不满足条件的路径,该断言总为false(可用于生成环境屏蔽 swagger)
                        //.ant("/user/**") // 满足字符串表达式路径
                        //.regex("") // 符合正则的路径
                )
                .build();
    }

    /**
     * 用于定义API主界面的信息,比如可以声明所有的API的总标题、描述、版本
     * @return
     */
    private ApiInfo apiInfo() {

        Contact contact = new Contact(
                "我是作者姓名", // 作者姓名
                "https://blog.csdn.net/", // 作者网址
                "123456789@163.com"); // 作者邮箱

        return new ApiInfoBuilder()
                .title("XX项目API") //  可以用来自定义API的主标题
                .description("XX项目SwaggerAPI管理") // 可以用来描述整体的API
                .termsOfServiceUrl("https://www.baidu.com") // 用于定义服务的域名(跳转链接)
                .version("1.0") // 可以用来定义版本
                .license("Swagger-的使用教程")
                .licenseUrl("https://blog.csdn.net")
                .contact(contact)
                .build(); //
    }
}

第三步:
在主程序入口加

import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@EnableWebMvc

用控制变量法研究了一下,
发现只有在我把@EnableWebMvc禁用后才会成功
查了它的功效:

@EnableWebMvc 注解的加入可能解决问题的原因通常涉及到Spring MVC的自动配置和默认行为。
当你在Spring Boot应用程序中使用@EnableWebMvc时,它告诉Spring Boot不要应用默认的Spring MVC自动配置,而是使用你提供的自定义配置。这对于某些特定的需求或问题是有用的,但要小心,因为这也可能导致一些默认配置失效。可能的原因和解释:
自定义MVC配置: 通过使用@EnableWebMvc,你可以完全掌控Spring MVC的配置。这对于需要更多自定义的场景是有帮助的,例如添加自定义的拦截器、视图解析器等。
默认配置的不兼容: Spring Boot的默认配置通常足够适用于典型的应用场景。然而,如果你的应用场景与默认配置不兼容,可能需要禁用默认配置并提供自己的配置。
解决冲突或覆盖默认配置: 在某些情况下,可能会存在依赖冲突或默认配置不符合特定需求的情况。通过使用@EnableWebMvc,你可以解决这些问题,确保应用程序的配置符合预期。
尽管@EnableWebMvc提供了更多的自定义能力,但在使用它之前,建议先理解清楚它会做什么,以及是否真的需要禁用默认配置。在大多数情况下,Spring Boot的默认配置能够满足应用程序的需求。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值