restjerseyjax-rsurl-parameters

When Jersey cannot map a query parameter, it fails with 404, why so?


I'm not a Jersey guru but I read that Jersey cannot resolve Java methods based on query params, but it looks sometimes it does, here is my example.

This is the server code:

@GET
@Path("/services")
public String getAll(
        @QueryParam("limit") Integer limit,
        @QueryParam("offset") Integer offset){
        return "1 2 3";
}

And this is the client code:

ClientResponse response = webResource
        .path("services")
        .queryParam("limit", "ab")
        .get(ClientResponse.class);
logger.info(response.toString());
assertEquals(response.getStatus(), 200);

It looks like Jersey doesn't like "ab" and isn't able to map the query param so it returns 404, however if limit = "1", i can hit the right method.

Is Jersey right to return 404 in this case? I know i could broaden the interface using String rather than Integer to override all the treatment for any feasible syntax error. Can I configure Jersey to do this on my behalf?

I'm using server: Grizzly/1.9.18, with Jersey 1.11


Solution

  • Currently this is not possible in Jersey. Maybe we could come up with a feature to make this more friendly. How about something like @ErrorParam annotation you can attach to a parameter. If such parameter is present and some QueryParam conversion fails, the query param would be populated with the default value and the real string value of the param in error would be added to the name-value map passed in the param annotated with @ErrorParam?

    @GET
    @Path("/services")
    public String getAll(
            @QueryParam("limit") Integer limit,
            @QueryParam("offset") Integer offset,
            @ErrorParam MultivaluedMap<String, String> typeErrors) {
    
        if (!typeErrors.isEmpty()) {
            // do something
        }
    
        return "1 2 3";
    }
    

    I filed an RFE here: https://github.com/eclipse-ee4j/jersey/issues/1535 (old, dead URL: http://java.net/jira/browse/JERSEY-1263 )

    Please comment if you have an opinion.