elunez/eladmin · error · BadRequestException

请先配置生成器

Error message

请先配置生成器

What it means

GeneratorServiceImpl.generator throws BadRequestException(CONFIG_MESSAGE = '请先配置生成器') when the GenConfig row for the table has a null id, meaning no generator configuration has been saved for that table yet. The config (package name, author, prefix, path) must exist before files can be generated.

Source

Thrown at eladmin-generator/src/main/java/me/zhengjie/service/impl/GeneratorServiceImpl.java:172

        for (ColumnInfo columnInfo : columnInfos) {
            // 根据字段名称查找
            List<ColumnInfo> columns = columnInfoList.stream().filter(c -> c.getColumnName().equals(columnInfo.getColumnName())).collect(Collectors.toList());
            // 如果找不到,就代表字段被删除了,则需要删除该字段
            if (CollectionUtil.isEmpty(columns)) {
                columnInfoRepository.delete(columnInfo);
            }
        }
    }

    @Override
    public void save(List<ColumnInfo> columnInfos) {
        columnInfoRepository.saveAll(columnInfos);
    }

    @Override
    public void generator(GenConfig genConfig, List<ColumnInfo> columns) {
        if (genConfig.getId() == null) {
            throw new BadRequestException(CONFIG_MESSAGE);
        }
        try {
            GenUtil.generatorCode(columns, genConfig);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new BadRequestException("生成失败,请手动处理已生成的文件");
        }
    }

    @Override
    public ResponseEntity<Object> preview(GenConfig genConfig, List<ColumnInfo> columns) {
        if (genConfig.getId() == null) {
            throw new BadRequestException(CONFIG_MESSAGE);
        }
        List<Map<String, Object>> genList = GenUtil.preview(columns, genConfig);
        return new ResponseEntity<>(genList, HttpStatus.OK);
    }

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Open the generator config page, fill in package/authors/API path for that table and save (PUT /api/genConfig), then retry.
  2. Verify via GET /api/genConfig/{tableName} that a config with a non-null id is returned before generating.
  3. If configs were wiped, re-enter them — check the gen_config table has a row matching the table name.

Example fix

// before: generate immediately after syncing columns
generatorService.generator(genConfigService.find(tableName), columns); // id == null -> 400

// after: persist config first, then generate
GenConfig config = genConfigService.find(tableName);
config.setPackName("me.zhengjie.mine");
genConfigService.update(config.getName(), config);   // now has id
generatorService.generator(config, columns);
Defensive patterns

Strategy: validation

Validate before calling

// Call the config endpoint first; save if missing
GenConfig config = genConfigService.find(tableName);
if (config == null || config.getId() == null) {
    config = defaultConfigFor(tableName);
    genConfigService.update(config.getTableName(), config); // persists, assigns id
}
generatorService.generator(config, columns);

Type guard

private boolean isConfigured(GenConfig c) {
    return c != null && c.getId() != null
        && StringUtils.isNotBlank(c.getPackName())
        && StringUtils.isNotBlank(c.getPath());
}

Try / catch

try {
    generatorService.generator(config, columns);
} catch (BadRequestException e) {
    if ("请先配置生成器".equals(e.getMessage())) { openConfigTab(tableName); return; }
    throw e;
}

Prevention

When it happens

Trigger: POST /api/generator/{tableName}/0 for a table whose gen_config record has not been created/saved (e.g. a newly synced table, or the config was deleted from the generator config screen).

Common situations: Fresh installations where the '代码生成' config tab was never filled; deleting the GenConfig row then clicking generate; multi-module setups where the config exists for another table but not the requested one.

Related errors


AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14). Data as JSON: /api/errors/66802209947d9336. Report an issue: GitHub.