Spring Security和Swagger2集成报错

在项目中使用了swagger来方便测试接口,但是事与愿违 ,在项目启动的时候竟然报错了

具体报错代码

RequestMappingHandlerMapping requestMappingHandlerMapping =  applicationContext.getBean(RequestMappingHandlerMapping.class);

报错内容如下:

Method springSecurityFilterChain in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration required a single bean, but 2 were found:
    - requestMappingHandlerMapping: defined by method 'requestMappingHandlerMapping' in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]
    - swagger2ControllerMapping: defined by method 'swagger2ControllerMapping' in class path resource [springfox/documentation/swagger2/configuration/Swagger2DocumentationConfiguration.class]

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping' available: expected single matching bean but found 2: requestMappingHandlerMapping,swagger2ControllerMapping

于是根据错误找到了对应的两个类

1.Swagger2DocumentationConfiguration

代码中显示,返回的PropertySourcedRequestMappingHandlerMapping是继承了RequestMappingHandlerMapping

 @Bean
  public HandlerMapping swagger2ControllerMapping(
      Environment environment,
      DocumentationCache documentationCache,
      ServiceModelToSwagger2Mapper mapper,
      JsonSerializer jsonSerializer) {
    return new PropertySourcedRequestMappingHandlerMapping(
        environment,
        new Swagger2Controller(environment, documentationCache, mapper, jsonSerializer));
  }

2.DelegatingWebMvcConfiguration

代码中显示返回的也是RequestMappingHandlerMapping

 /**
     * Return a {@link RequestMappingHandlerMapping} ordered at 0 for mapping
     * requests to annotated controllers.
     */
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping mapping = createRequestMappingHandlerMapping();
                ......
        }

所以spring在配置文件中加载的时候看到了两个RequestMappingHandlerMapping不知道使用哪一个所以报错了【审判庭加载的时候是按照类型进行加载的】

3.解决办法

1.如果是非maven说模块项目,责把

RequestMappingHandlerMapping requestMappingHandlerMapping =  applicationContext.getBean(RequestMappingHandlerMapping.class);改为
RequestMappingHandlerMapping requestMappingHandlerMapping =  (RequestMappingHandlerMapping )applicationContext.getBean("requestMappingHandlerMapping");

2.如果是多模块项目 责把swagger所在的模块放在Spring Security所在模块之后也可以解决问题

<think>好的,用户需要一个关于“migration testing plan template IT project”的帮助。首先,我需要理解用户的需求。用户可能正在计划一个IT系统的迁移项目,需要创建一个测试方案模板来确保迁移过程顺利。根据用户提供的引用,迁移在计算机系统中指改变、程序或硬件的迁移或转移[^1]。同时,引用中提到了数据库API的CRUD操作,涉及用户数据、活动日志和推荐的处理[^2],这可能与迁移测试中的数据迁移和功能验证有关。 接下来,我应该考虑迁移测试计划的关键组成部分。通常,测试计划需要包括目标、范围、测试类型、环境、工具、时间安排、风险评估和报告等部分。需要确保涵盖单元测试、集成测试、系统测试和验收测试等不同层次。此外,数据迁移的完整性、性能测试和回滚计划也是重点。 用户可能需要具体的模板结构,所以需要分步骤列出各个部分,并提供示例。比如,测试范围应明确迁移的模块,如数据库、应用程序、配置等。测试类型可能包括数据验证、功能测试、性能测试等。工具方面,可以提到常用的测试工具和自动化框架,如Selenium、Postman、JMeter等,以帮助用户实际操作。 还需要注意引用用户提供的资料,比如在数据迁移部分引用CRUD操作[^2],强调数据完整性的验证。同时,风险评估部分要提到数据丢失或系统兼容性问题,这与迁移的定义相关。 最后,生成相关问题时要围绕迁移测试计划的扩展内容,如具体测试用例编写、自动化工具选择、回滚流程设计等,帮助用户进一步深入。 总结步骤: 1. 确定模板结构:目标、范围、测试类型等。 2. 填充各部分内容,结合引用中的关键点。 3. 提供示例模板和工具建议。 4. 生成相关问题,引导用户深入探讨各环节。</think>IT项目迁移测试方案模板应包含系统兼容性验证、数据完整性检查、业务连续性保障三大核心模块。以下是迁移测试计划的核心要素及模板框架: $$风险评估矩阵 = \begin{bmatrix} 风险类型 & 概率 & 影响值 & 缓解措施 \\ 数据丢失 & 0.3 & 9 & 实施双写机制 \\ API兼容性 & 0.5 & 7 & 版本回归测试 \\ 性能下降 & 0.4 & 8 & 预压测优化 \end{bmatrix}$$ 一、**测试范围定义** 1. 源系统与目标系统版本对比表 2. 迁移对象清单(数据库表$D=\{t_1,t_2,...,t_n\}$、配置文件、静态资源) 3. 关键接口映射关系$f: API_{old} \rightarrow API_{new}$ 二、**测试策略设计** ```python # 数据迁移验证示例 def validate_migration(source_db, target_db): for table in schema.tables: src_count = source_db.execute(f"SELECT COUNT(*) FROM {table}") tgt_count = target_db.execute(f"SELECT COUNT(*) FROM {table}") assert src_count == tgt_count, f"数据不一致表: {table}" validate_crud_operations(table) # 引用CRUD操作验证 ``` 三、**测试类型矩阵** | 测试阶段 | 验证目标 | 工具选择 | 成功标准 | |----------------|--------------------------|-------------------|-------------------| | 单元测试 | 组件级功能 | JUnit/Postman | 代码覆盖率≥85% | | 集成测试 | 系统交互 | SoapUI/LoadRunner | 事务成功率100% | | 性能测试 | 响应时间TPS | JMeter/Gatling | ≤2秒P95响应 | | 回滚测试 | 系统还原能力 | Ansible/Terraform | 30分钟完整回滚 | 四、**实施路线图** 1. 预迁移检查清单 2. 增量数据同步窗口$W=[T_1,T_2]$定义 3. 监控指标集$M=\{CPU_{util}, IO_{latency}, QPS\}$ **模板应用示例**: 1. 数据迁移测试用例应包含: - 边界值测试:空表/大表(记录数$n\geq10^6$)迁移 - 关系完整性:外键约束$FK\subseteq PK$验证 - 数据转换:编码格式$charset(src)\rightarrow charset(dest)$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值