1
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/greeting/{name}")
public Uni<String> greeting(String name, @QueryParam("limit") int limit) {
     return service.greeting(name.toString());
}

localhost:8080/greeting/someName?limit=454 -- this returns 200 as expected localhost:8080/greeting/someName?limit=dfg -- this one returns 404 instead of 400

In a Quarkus application an endpoint returns wrong error code(404 instead of 400)

But in a Spring boot non reactive application this works fine(returns 400)

  • 1
    What if you change the `limit` to a String and parse that String in your code? It sounds like Quarkus is doing the right thing - there is no resource that takes a String, only one that takes an int. – stdunbar Apr 27 '23 at 16:06

2 Answers2

1

Query param conversion errors are treaten as 404 (and not as 400 as expected) as for specification (https://eclipse-ee4j.github.io/jersey.github.io/documentation/latest3x/jaxrs-resources.html#d0e2052).

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69
  • Is that counter-intuitve only for me? What if user put some dumb data that cannot be converted? Does it have a way to revert this behavior? – Stanislav Trifan Aug 18 '23 at 09:57
0

I think quarkus opted to treat this as error 404 because it's expecting a method with the following signature:

public Uni<String> greeting(String name, @QueryParam("limit") String limit) {
   ...
}

and it doesn't find one.

One way to solve your use case is by reading the limit as a string and then converting it into an integer:


    // Convert a NumberFormatException error into a 400 status code response
    @ServerExceptionMapper
    public RestResponse<String> mapException(NumberFormatException x) {
        return RestResponse.status(Response.Status.BAD_REQUEST, "Unknown limit: " + x.getMessage());
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/greeting/{name}")
    public Uni<String> greeting(String name, @QueryParam("limit") String limit) {
        // Throws NumberFormatException if the limit is not a number
        int limitAsInt = Integer.parseInt( limit );
        return service.greeting(name.toString());
    }
Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30