elunez/eladmin · warning · BadRequestException

A new job cannot already have an ID

Error message

A new job cannot already have an ID

What it means

Thrown by JobController.createJob (line 71) when POST /api/job is called with an id in the body. Positions (岗位) follow the standard eladmin create contract: identity is generated server-side, and an id on create is rejected with 400 before jobService.create touches the database.

Source

Thrown at eladmin-system/src/main/java/me/zhengjie/modules/system/rest/JobController.java:71

    @PreAuthorize("@el.check('job:list')")
    public void exportJob(HttpServletResponse response, JobQueryCriteria criteria) throws IOException {
        jobService.download(jobService.queryAll(criteria), response);
    }

    @ApiOperation("查询岗位")
    @GetMapping
    @PreAuthorize("@el.check('job:list','user:list')")
    public ResponseEntity<PageResult<JobDto>> queryJob(JobQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(jobService.queryAll(criteria, pageable),HttpStatus.OK);
    }

    @Log("新增岗位")
    @ApiOperation("新增岗位")
    @PostMapping
    @PreAuthorize("@el.check('job:add')")
    public ResponseEntity<Object> createJob(@Validated @RequestBody Job resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        jobService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改岗位")
    @ApiOperation("修改岗位")
    @PutMapping
    @PreAuthorize("@el.check('job:edit')")
    public ResponseEntity<Object> updateJob(@Validated(Job.Update.class) @RequestBody Job resources){
        jobService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除岗位")
    @ApiOperation("删除岗位")
    @DeleteMapping
    @PreAuthorize("@el.check('job:del')")

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Strip id from the request body before POST /api/job.
  2. Reset the job form model (id = null) on dialog open for create mode.
  3. Use PUT /api/job with the existing id when the intent is actually an update.

Example fix

// before
axios.post('/api/job', this.form)
// after
const { id, ...payload } = this.form;
axios.post('/api/job', payload)
Defensive patterns

Strategy: validation

Validate before calling

const payload = { name: form.name, enabled: form.enabled, sort: form.sort }; // no id
axios.post('/api/job', payload);

Type guard

const isCreateSafe = (job) => job.id == null;

Prevention

When it happens

Trigger: Job dialog not reset between editing an existing position and creating one; POSTing a JobDto obtained from the paginated GET /api/job list; migration scripts inserting positions from another system with source ids included.

Common situations: Shared add/edit dialog with leftover form state; testers copying a PUT body into POST; scripts syncing sys_job between environments.

Related errors


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