elunez/eladmin · warning · BadRequestException

A new dict cannot already have an ID

Error message

A new dict cannot already have an ID

What it means

Thrown by DictController.createDict (line 80) when POST /api/dict includes a non-null id in the JSON body. Same eladmin create-contract guard as other controllers: dictionary entries get their identity from the database, and an inbound id on create is rejected with 400 before dictService.create runs.

Source

Thrown at eladmin-system/src/main/java/me/zhengjie/modules/system/rest/DictController.java:80

    @PreAuthorize("@el.check('dict:list')")
    public ResponseEntity<List<DictDto>> queryAllDict(){
        return new ResponseEntity<>(dictService.queryAll(new DictQueryCriteria()),HttpStatus.OK);
    }

    @ApiOperation("查询字典")
    @GetMapping
    @PreAuthorize("@el.check('dict:list')")
    public ResponseEntity<PageResult<DictDto>> queryDict(DictQueryCriteria resources, Pageable pageable){
        return new ResponseEntity<>(dictService.queryAll(resources,pageable),HttpStatus.OK);
    }

    @Log("新增字典")
    @ApiOperation("新增字典")
    @PostMapping
    @PreAuthorize("@el.check('dict:add')")
    public ResponseEntity<Object> createDict(@Validated @RequestBody Dict resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        dictService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改字典")
    @ApiOperation("修改字典")
    @PutMapping
    @PreAuthorize("@el.check('dict:edit')")
    public ResponseEntity<Object> updateDict(@Validated(Dict.Update.class) @RequestBody Dict resources){
        dictService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除字典")
    @ApiOperation("删除字典")
    @DeleteMapping
    @PreAuthorize("@el.check('dict:del')")

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Delete the id key from the request payload before calling POST /api/dict.
  2. Reset the form bound object ({ id: null, name: '', remark: '' }) when opening the create dialog.
  3. For cross-environment dict sync, use PUT with existing ids or write a dedicated import endpoint instead of POST-with-id.

Example fix

// before
axios.post('/api/dict', row) // row came from the list with id set
// after
const { id, ...payload } = row;
axios.post('/api/dict', payload)
Defensive patterns

Strategy: validation

Validate before calling

const payload = { name: form.name, remark: form.remark }; // whitelist fields for create
axios.post('/api/dict', payload);

Type guard

const isCreatePayload = (p) => !('id' in p) || p.id == null;

Prevention

When it happens

Trigger: Reusing the dict edit form (populated with id) for creation; POSTing a DictDto fetched from GET /api/dict back to the create endpoint; bulk-loading dict rows from another environment without removing ids.

Common situations: Front-end dialog state not cleared between edit and create; sync scripts copying dict data between dev/prod instances; Swagger testing where the example schema includes id and the tester leaves it filled.

Related errors


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