binarywang/WxJava · error · RuntimeException

请确保微信视频号配置 appId 的唯一性

Error message

请确保微信视频号配置 appId 的唯一性

What it means

Thrown by the Spring Boot multi-account Channel (视频号) starter when two tenant entries resolve to the same appId, to prevent Redis-based token/ticket cache key collisions (see WxChannelRedisConfigImpl#setAppid). It is a RuntimeException that aborts bean creation at startup.

Source

Thrown at spring-boot-starters/wx-java-channel-multi-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/channel/configuration/services/AbstractWxChannelConfiguration.java:51

    Map<String, WxChannelSingleProperties> appsMap = wxChannelMultiProperties.getApps();
    if (appsMap == null || appsMap.isEmpty()) {
      log.warn("微信视频号应用参数未配置,通过 WxChannelMultiServices#getWxChannelService(\"tenantId\")获取实例将返回空");
      return new WxChannelMultiServicesImpl();
    }
    /**
     * 校验 appId 是否唯一,避免使用 redis 缓存 token、ticket 时错乱。
     *
     * 查看 {@link me.chanjar.weixin.channel.config.impl.WxChannelRedisConfigImpl#setAppid(String)}
     */
    Collection<WxChannelSingleProperties> 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 的唯一性");
      }
    }
    WxChannelMultiServicesImpl services = new WxChannelMultiServicesImpl();

    Set<Map.Entry<String, WxChannelSingleProperties>> entries = appsMap.entrySet();
    for (Map.Entry<String, WxChannelSingleProperties> entry : entries) {
      String tenantId = entry.getKey();
      WxChannelSingleProperties wxChannelSingleProperties = entry.getValue();
      WxChannelDefaultConfigImpl storage = this.wxChannelConfigStorage(wxChannelMultiProperties);
      this.configApp(storage, wxChannelSingleProperties);
      this.configHttp(storage, wxChannelMultiProperties.getConfigStorage());
      WxChannelService wxChannelService = this.wxChannelService(storage, wxChannelMultiProperties);
      services.addWxChannelService(tenantId, wxChannelService);
    }
    return services;
  }

  /**

View on GitHub (pinned to 1c43293a3c)

Solutions

  1. List all `wx.channel.apps.*.appId` values and deduplicate.
  2. Make each tenant's appId unique; remove blanks.
  3. Restart; the check only passes when no grouping key has count > 1.

Example fix

# before
wx.channel.apps:
  tenant-a: { appId: wxch001 }
  tenant-b: { appId: wxch001 }
# after
wx.channel.apps:
  tenant-a: { appId: wxch001 }
  tenant-b: { appId: wxch002 }
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Two entries under `wx.channel.apps` (or `wxjava.channel.apps`) with the same `appId`; two entries with null appId (both collapse to grouping key `0`).

Common situations: Duplicated tenant YAML blocks; one appId shared by mistake; migrating from single- to multi-account and leaving the same appId on two keys.

Related errors


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