binarywang/WxJava · error · RuntimeException

请确保微信公众号配置 appId 的唯一性

Error message

请确保微信公众号配置 appId 的唯一性

What it means

Thrown by the Spring Boot multi-account Official Account (公众号/MP) starter when two tenants share the same appId, to avoid Redis token/ticket cache collisions (see WxMpRedisConfigImpl#setAppId). After the uniqueness check the starter branches into SHARED vs ISOLATED multi-tenant mode.

Source

Thrown at spring-boot-starters/wx-java-mp-multi-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/mp/configuration/services/AbstractWxMpConfiguration.java:58

    if (appsMap == null || appsMap.isEmpty()) {
      log.warn("微信公众号应用参数未配置,通过 WxMpMultiServices#getWxMpService(\"tenantId\")获取实例将返回空");
      return new WxMpMultiServicesImpl();
    }
    
    /**
     * 校验 appId 是否唯一,避免使用 redis 缓存 token、ticket 时错乱。
     *
     * 查看 {@link me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl#setAppId(String)}
     */
    Collection<WxMpSingleProperties> apps = appsMap.values();
    if (apps.size() > 1) {
      // 校验 appId 是否唯一
      boolean multi = apps.stream()
        // 没有 appId,如果不判断是否为空,这里会报 NPE 异常
        .collect(Collectors.groupingBy(c -> c.getAppId() == null ? 0 : c.getAppId(), Collectors.counting()))
        .entrySet().stream().anyMatch(e -> e.getValue() > 1);
      if (multi) {
        throw new RuntimeException("请确保微信公众号配置 appId 的唯一性");
      }
    }

    // 根据配置选择多租户模式
    WxMpMultiProperties.MultiTenantMode mode = wxMpMultiProperties.getConfigStorage().getMultiTenantMode();
    if (mode == WxMpMultiProperties.MultiTenantMode.SHARED) {
      return createSharedMultiServices(appsMap, wxMpMultiProperties);
    } else {
      return createIsolatedMultiServices(appsMap, wxMpMultiProperties);
    }
  }

  /**
   * 创建隔离模式的多租户服务(每个租户独立 WxMpService 实例)
   */
  private WxMpMultiServices createIsolatedMultiServices(
    Map<String, WxMpSingleProperties> appsMap,
    WxMpMultiProperties wxMpMultiProperties) {

View on GitHub (pinned to 1c43293a3c)

Solutions

  1. Make each `wx.mp.apps.*.appId` distinct and non-blank.
  2. Restart; then confirm the `multiTenantMode` setting is correct for your setup.

Example fix

# before
wx.mp.apps:
  a: { appId: wxmp001 }
  b: { appId: wxmp001 }
# after
wx.mp.apps:
  a: { appId: wxmp001 }
  b: { appId: wxmp007 }
Defensive patterns

Strategy: validation

Validate before calling

// Spring Boot MP multi: pre-flight appId uniqueness
Set<String> seen = new HashSet<>();
for (WxMpSingleProperties p : appsMap.values()) {
    if (p.getAppId() == null || !seen.add(p.getAppId())) {
        throw new IllegalStateException("Duplicate/null MP appId");
    }
}

Prevention

When it happens

Trigger: Two entries under `wx.mp.apps` with the same `appId`; two entries with null appId (collapsed to key `0`).

Common situations: Same Official Account appId configured twice; templated YAML where appId placeholder was not replaced.

Related errors


AI-assisted analysis of binarywang/WxJava@1c43293a3c (2026-08-14). Data as JSON: /api/errors/67391ddd66535238. Report an issue: GitHub.