apolloconfig/apollo · error · BadRequestException

Consumer already exist

Error message

Consumer already exist

What it means

Thrown as BadRequestException (HTTP 400) by ConsumerService.createConsumer when a Consumer already exists for the given appId (consumerRepository.findByAppId returns non-null). Each Apollo appId can have at most one OpenAPI Consumer (token holder).

Source

Thrown at apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/service/ConsumerService.java:104

      final RoleRepository roleRepository) {
    this.consumerTokenRepository = consumerTokenRepository;
    this.consumerRepository = consumerRepository;
    this.consumerAuditRepository = consumerAuditRepository;
    this.consumerRoleRepository = consumerRoleRepository;
    this.portalConfig = portalConfig;
    this.rolePermissionService = rolePermissionService;
    this.userService = userService;
    this.roleRepository = roleRepository;
  }


  public Consumer createConsumer(Consumer consumer, String operator) {
    validateOperator(operator);
    String appId = consumer.getAppId();

    Consumer managedConsumer = consumerRepository.findByAppId(appId);
    if (managedConsumer != null) {
      throw new BadRequestException("Consumer already exist");
    }

    String ownerName = consumer.getOwnerName();
    UserInfo owner = userService.findByUserId(ownerName);
    if (owner == null) {
      throw BadRequestException.userNotExists(ownerName);
    }
    consumer.setOwnerEmail(owner.getEmail());

    consumer.setDataChangeCreatedBy(operator);
    consumer.setDataChangeLastModifiedBy(operator);

    return consumerRepository.save(consumer);
  }

  public ConsumerToken generateAndSaveConsumerToken(Consumer consumer, Integer rateLimit,
      Date expires, String operator) {
    validateOperator(operator);

View on GitHub (pinned to d95fc18d11)

Solutions

  1. Reuse the existing Consumer (look it up by appId) instead of creating a new one.
  2. Delete the existing Consumer first if you truly need to recreate it.
  3. Make provisioning scripts idempotent: findByAppId first, create only if absent.

Example fix

// before
consumerService.createConsumer(consumer, operator); // 400 if appId exists
// after
Consumer existing = consumerRepository.findByAppId(appId);
if (existing != null) {
  return existing;
}
return consumerService.createConsumer(consumer, operator);
Defensive patterns

Strategy: validation

Validate before calling

Consumer existing = consumerRepository.findByAppId(appId);
if (existing == null) {
  consumerService.createConsumer(consumer, operator);
} else {
  return existing;
}

Try / catch

try {
  return consumerService.createConsumer(consumer, operator);
} catch (BadRequestException e) {
  if ("Consumer already exist".equals(e.getMessage())) {
    return consumerRepository.findByAppId(appId);
  }
  throw e;
}

Prevention

When it happens

Trigger: Creating a Consumer for an appId that already has a Consumer row; re-running a consumer-creation script against the same appId.

Common situations: See trigger scenarios.

Related errors


AI-assisted analysis of apolloconfig/apollo@d95fc18d11 (2026-08-14). Data as JSON: /api/errors/a14e01f24b9c412d. Report an issue: GitHub.