quarkusio/quarkus · error · WebApplicationException

Id was invalidly set on request.

Error message

Id was invalidly set on request.

What it means

This REST resource rejects POST requests whose Fruit payload already carries a non-null id. The server assigns identities on create, so a client-supplied id would silently override or collide with the generated key; the resource throws WebApplicationException with HTTP 422 (Unprocessable Entity) to force the client to omit it.

Solutions

  1. Remove the id field (or set it to null) from the JSON body before POSTing
  2. In the client, keep separate DTOs/models for create (no id) and update (with id)
  3. Use JsonInclude.NON_NULL / @JsonInclude so an unset id is not serialized
  4. Clear the id on the object before persisting: fruit.setId(null)

Example fix

// before
POST /fruits
{"id": 5, "name": "Apple"}

// after
POST /fruits
{"name": "Apple"}
Defensive patterns

Strategy: validation

Validate before calling

if (fruit.getId() != null) {
    throw new IllegalArgumentException("id must not be set when creating a fruit");
}

Type guard

boolean isValidForCreate(Fruit f) {
    return f != null && f.getId() == null && f.getName() != null;
}

Try / catch

try {
    Response r = target.request().post(Entity.json(newFruit));
    if (r.getStatus() == 422) { /* strip id and retry */ }
} catch (WebApplicationException e) {
    if (e.getResponse().getStatus() == 422) { fruit.setId(null); }
}

Prevention

When it happens

Trigger: POST /fruits with a JSON body containing a non-null "id" field, e.g. re-posting an entity previously fetched via GET instead of sending a fresh object.

Common situations: Clients copying an existing JSON record and re-posting it; test code reusing a fixture object after persisting it once; deserializers that default numeric id fields to 0/null inconsistently; frontend forms that include the id input even in create mode.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/120f123573be009a. Report an issue: GitHub.

Appendix: source

Thrown at integration-tests/hibernate-search-orm-elasticsearch-tenancy/src/main/java/io/quarkus/it/hibernate/search/orm/elasticsearch/multitenancy/fruit/FruitResource.java:62

    }

    @GET
    @Path("/{id}")
    @Transactional
    public Fruit findById(int id) {
        Fruit entity = entityManager.find(Fruit.class, id);
        if (entity == null) {
            throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
        }
        return entity;
    }

    @POST
    @Path("/")
    @Transactional
    public Response create(@NotNull Fruit fruit) {
        if (fruit.getId() != null) {
            throw new WebApplicationException("Id was invalidly set on request.", 422);
        }
        LOG.debugv("Create {0}", fruit.getName());
        entityManager.persist(fruit);
        return Response.ok(fruit).status(Response.Status.CREATED).build();
    }

    @PUT
    @Path("/{id}")
    @Transactional
    public Fruit update(@NotNull @PathParam("id") int id, @NotNull Fruit fruit) {
        if (fruit.getName() == null) {
            throw new WebApplicationException("Fruit Name was not set on request.", 422);
        }

        Fruit entity = entityManager.find(Fruit.class, id);
        if (entity == null) {
            throw new WebApplicationException("Fruit with id of " + id + " does not exist.", 404);
        }

View on GitHub (pinned to e1c734241f)