elunez/eladmin · error · BadRequestException

A new quartzJob cannot already have an ID

Error message

A new quartzJob cannot already have an ID

What it means

Standard eladmin REST guard in QuartzJobController.createQuartzJob: a POST /api/quartz/jobs body whose JSON includes a non-null 'id' is rejected because creating a new entity must not carry a primary key. The id is normally auto-generated by JPA, so a client-supplied id indicates the wrong endpoint or a stale frontend payload.

Source

Thrown at eladmin-system/src/main/java/me/zhengjie/modules/quartz/rest/QuartzJobController.java:88

    @PreAuthorize("@el.check('timing:list')")
    public void exportQuartzJobLog(HttpServletResponse response, JobQueryCriteria criteria) throws IOException {
        quartzJobService.downloadLog(quartzJobService.queryAllLog(criteria), response);
    }

    @ApiOperation("查询任务执行日志")
    @GetMapping(value = "/logs")
    @PreAuthorize("@el.check('timing:list')")
    public ResponseEntity<PageResult<QuartzLog>> queryQuartzJobLog(JobQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(quartzJobService.queryAllLog(criteria,pageable), HttpStatus.OK);
    }

    @Log("新增定时任务")
    @ApiOperation("新增定时任务")
    @PostMapping
    @PreAuthorize("@el.check('timing:add')")
    public ResponseEntity<Object> createQuartzJob(@Validated @RequestBody QuartzJob resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        // 验证Bean是不是合法的,合法的定时任务 Bean 需要用 @Service 定义
        checkBean(resources.getBeanName());
        quartzJobService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改定时任务")
    @ApiOperation("修改定时任务")
    @PutMapping
    @PreAuthorize("@el.check('timing:edit')")
    public ResponseEntity<Object> updateQuartzJob(@Validated(QuartzJob.Update.class) @RequestBody QuartzJob resources){
        // 验证Bean是不是合法的,合法的定时任务 Bean 需要用 @Service 定义
        checkBean(resources.getBeanName());
        quartzJobService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

View on GitHub (pinned to 55fbf70595)

Solutions

  1. Remove the 'id' field from the request body (or set it to null) when calling POST /api/quartz/jobs.
  2. Use PUT /api/quartz/jobs when you actually intend to update an existing job.
  3. In the frontend form, reset the quartzJob object to a blank template when opening the 'new' dialog.

Example fix

// before
POST /api/quartz/jobs
{"id": 3, "jobName": "job", "cronExpression": "0 0 * * * ?", "beanName": "runTaskJob"}
// after
POST /api/quartz/jobs
{"jobName": "job", "cronExpression": "0 0 * * * ?", "beanName": "runTaskJob"}
Defensive patterns

Strategy: validation

Validate before calling

// client-side guard before POST
if (job.getId() != null) { throw new Error('Send PUT /api/quartz/jobs to update, or clear id to create'); }
await axios.post('/api/quartz/jobs', job);

Prevention

When it happens

Trigger: POST /api/quartz/jobs with a body like {"id":5,"jobName":"x",...}. Commonly caused by a frontend 'add' form reusing an edited job object, or by copy-pasting an update (PUT) request body into a create (POST) request.

Common situations: Frontend edit dialog not clearing the id field before switching to create mode; API clients scripting job creation from an exported job list; tools that echo back the full object on save.

Related errors


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