elunez/eladmin · warning · BadRequestException
A new dictDetail cannot already have an ID
Error message
A new dictDetail cannot already have an ID
What it means
Thrown by DictDetailController.createDictDetail (line 77) when the POST /api/dictDetail body contains an id. Guard runs ahead of dictDetailService.create; dict details are owned by their parent dict and get server-assigned ids, so a client-supplied id on create is a contract violation returning 400.
Source
Thrown at eladmin-system/src/main/java/me/zhengjie/modules/system/rest/DictDetailController.java:77
@ApiOperation("查询多个字典详情")
@GetMapping(value = "/map")
public ResponseEntity<Object> getDictDetailMaps(@RequestParam String dictName){
String[] names = dictName.split("[,,]");
Map<String, List<DictDetailDto>> dictMap = new HashMap<>(16);
for (String name : names) {
dictMap.put(name, dictDetailService.getDictByName(name));
}
return new ResponseEntity<>(dictMap, HttpStatus.OK);
}
@Log("新增字典详情")
@ApiOperation("新增字典详情")
@PostMapping
@PreAuthorize("@el.check('dict:add')")
public ResponseEntity<Object> createDictDetail(@Validated @RequestBody DictDetail resources){
if (resources.getId() != null) {
throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
}
dictDetailService.create(resources);
return new ResponseEntity<>(HttpStatus.CREATED);
}
@Log("修改字典详情")
@ApiOperation("修改字典详情")
@PutMapping
@PreAuthorize("@el.check('dict:edit')")
public ResponseEntity<Object> updateDictDetail(@Validated(DictDetail.Update.class) @RequestBody DictDetail resources){
dictDetailService.update(resources);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@Log("删除字典详情")
@ApiOperation("删除字典详情")
@DeleteMapping(value = "/{id}")
@PreAuthorize("@el.check('dict:del')")View on GitHub (pinned to 55fbf70595)
Solutions
- Remove id from the payload: destructure or delete payload.id before POST /api/dictDetail.
- Clear the detail form (including id) each time the create dialog opens.
- Keep dict and id in separate variables in the dialog state so create never sees the edit row's id.
Example fix
// before
this.$axios.post('/api/dictDetail', this.detailForm) // id left from edited row
// after
const { id, ...payload } = this.detailForm;
this.$axios.post('/api/dictDetail', payload) Defensive patterns
Strategy: validation
Validate before calling
const { id, ...payload } = detailForm;
axios.post('/api/dictDetail', { ...payload, dict: { id: parentDictId } }); Type guard
const canCreate = (d) => d.id === null || d.id === undefined;
Prevention
- Bind the create dialog to a fresh object, not the selected detail row.
- Pass the parent dict id explicitly rather than reusing a fetched DictDetail.
When it happens
Trigger: Submitting the dict-detail edit form in create mode without clearing id; round-tripping a DictDetail from GET /api/dictDetail into POST; front-end detail list re-using row objects for the 'add detail' dialog.
Common situations: Dialog component shared between edit and create without a form reset; importing dict details exported from another instance; naive copy of the PUT payload into a POST during API testing.
Related errors
- A new dept cannot already have an ID
- A new dict cannot already have an ID
- A new job cannot already have an ID
- A new menu cannot already have an ID
- A new role cannot already have an ID
AI-assisted analysis of elunez/eladmin@55fbf70595 (2026-08-14).
Data as JSON: /api/errors/d89611f2a38a8d62.
Report an issue: GitHub.