jeancallisti
jeancallisti

Reputation: 1663

Can I use a custom parser for QueryParams?

Context :

I am using Jakarta with RestEasy. I have a simple endpoint such as this one :

@GET
@Path("expectsLong")
@Produces(MediaType.TEXT_PLAIN)
public Response expectsLong(
    @QueryParam("searchValue") final Long searchValue)
{
    return ok(searchValue);
}

I am focused on applying some validation with annotations only (I'm not following the ParamConverter approach). For example :

@NotNull @Min(1) @Max(10) @QueryParam("searchValue") final Long searchValue)

By design, all of those annotations come into effect only after the initial step, which is the parsing from String to Long.

  1. Step 1 : PARSING "666"  ->  666
  2. Step 2 : @NotNull    ->  Is 666 null? -> no -> valid
  3. Step 3 : @Min(1) -> Is 666 >= 1? -> yes -> valid
  4. Step 4 : @Max(10) -> Is 666 <= 10? -> yes -> valid

(Side note: If you try to use an annotation that has nothing to do with the expected parameter's type (for example: If you tried to use @Pattern on searchValue, which is a Long) then you would get the infamous error HV000030: No validator could be found for constraint)

Problem :

The parsing step is a bit of an alien in that flow:

My approach :

It seems to me that I need to customize the very first step, the parsing step.

Question :

is it possible to provide a custom parser to RestEasy?

Upvotes: 0

Views: 30

Answers (0)

Related Questions