Search code examples
apirestquarkusmicroprofile

Validate REST parameter in Quarkus Microprofile


The following code is part of a controller in a Quarkus Microprofile API application.

    @GET
    @Path("/limit/{limit}/offset/{offset}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response paginatedAccounts(
            @Parameter(
                description = "Number of records to be returned.",
                required = true,
                schema = @Schema(type = SchemaType.INTEGER))
            @PathParam("limit") int limit,
            @Parameter(
                 description = "The starting number of record, zero based.",
                 required = true,
                 schema = @Schema(type = SchemaType.INTEGER))
             @PathParam("offset") int offset)
    {
        return Response
                .ok(this.accountService.getPaginatedAccounts(limit, offset))
                .build();
    }

It returns a paginated list of accounts.

When user calls the API providing a wrong type for "limit" or "offset", ie:

http://[url]/[entity]/limit/zzz/offset/0

she receives "404 - Not Found"

How to validate the parameters "limit" and "offset" so that when user supplies a wrong type (string for int) she receives instead:

"400 - Bad Request"

as it should be?


Solution

  • This is by design (of the JAX-RS spec).

    https://docs.oracle.com/cd/E19798-01/821-1841/6nmq2cp1v/index.html mentions it explicitly:

    If the URI path template variable cannot be cast to the specified type, the JAX-RS runtime returns an HTTP 400 (“Bad Request”) error to the client. If the @PathParam annotation cannot be cast to the specified type, the JAX-RS runtime returns an HTTP 404 (“Not Found”) error to the client