elunez/eladmin · warning · BadRequestException

此环境不允许生成代码,请选择预览或者下载查看!

Error message

此环境不允许生成代码,请选择预览或者下载查看!

What it means

GeneratorController.generatorCode throws this when the code-generation action (type == 0) is requested but the generatorEnabled flag (eladmin generator.enabled, defaults false in prod configs) is false. Types 1 (preview) and 2 (download zip) remain available, hence the message telling you to preview or download instead. It is a safety switch so production systems never write generated files to disk.

Source

Thrown at eladmin-generator/src/main/java/me/zhengjie/rest/GeneratorController.java:94

    public ResponseEntity<HttpStatus> saveColumn(@RequestBody List<ColumnInfo> columnInfos){
        generatorService.save(columnInfos);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("同步字段数据")
    @PostMapping(value = "sync")
    public ResponseEntity<HttpStatus> syncColumn(@RequestBody List<String> tables){
        for (String table : tables) {
            generatorService.sync(generatorService.getColumns(table), generatorService.query(table));
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("生成代码")
    @PostMapping(value = "/{tableName}/{type}")
    public ResponseEntity<Object> generatorCode(@PathVariable String tableName, @PathVariable Integer type, HttpServletRequest request, HttpServletResponse response){
        if(!generatorEnabled && type == 0){
            throw new BadRequestException("此环境不允许生成代码,请选择预览或者下载查看!");
        }
        switch (type){
            // 生成代码
            case 0: generatorService.generator(genConfigService.find(tableName), generatorService.getColumns(tableName));
                    break;
            // 预览
            case 1: return generatorService.preview(genConfigService.find(tableName), generatorService.getColumns(tableName));
            // 打包
            case 2: generatorService.download(genConfigService.find(tableName), generatorService.getColumns(tableName), request, response);
                    break;
            default: throw new BadRequestException("没有这个选项");
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Use the frontend buttons '预览' (type=1) or '下载' (type=2) which are always allowed and give the same output without writing files.
  2. If you truly need server-side generation, set generator.enabled: true in the active configuration and restart the service.
  3. Better practice: download the zip (type=2) and generate locally, keeping the switch off in shared environments.

Example fix

# before (eladmin.yml, prod)
generator:
  enabled: false

# after
generator:
  enabled: true   # restart required; prefer download (type=2) instead
Defensive patterns

Strategy: validation

Validate before calling

// Client: choose an allowed type when generation is disabled
boolean generationEnabled = false; // from /api/generator config or deployment knowledge
int type = generationEnabled ? 0 : 2; // download zip instead of server-side generate

Type guard

boolean isAllowedGeneratorType(boolean generatorEnabled, int type) {
    return type == 1 || type == 2 || (type == 0 && generatorEnabled);
}

Try / catch

try {
    return post("/api/generator/" + table + "/0");
} catch (BadRequestException e) {
    if (e.getMessage().contains("不允许生成代码")) {
        return post("/api/generator/" + table + "/2"); // fallback to download
    }
    throw e;
}

Prevention

When it happens

Trigger: POST /api/generator/{tableName}/0 (generate into the project) while generator.enabled is false in the active profile's yml. Typical after deploying eladmin with the production config where code generation is deliberately disabled.

Common situations: Running against the prod profile where generator.enabled: false; copying eladmin.yml from a deployment template that disables generation; wanting to generate code on a server without editing the config.

Related errors


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