JBoss.orgCommunity Documentation

Chapter 14. Optional parameter types

RESTEasy offers a mechanism to support a series of java.util.Optional types as a wrapper object types. This will give users the ability to use optional typed parameters, and eliminate all null checks by using methods like Optional.orElse().

Here is the sample:

@Path("/double")
@GET
public String optDouble(@QueryParam("value") OptionalDouble value) {
    return Double.toString(value.orElse(4242.0));
}

From the above sample code we can see that the OptionalDouble can be used as parameter type, and when users don't provide a value in @QueryParam, then the default value will be returned.

Here is the list of supported optional parameter types:

  • @QueryParam

  • @FormParam

  • @MatrixParam

  • @HeaderParam

  • @CookieParam

As the list shown above, those parameter types support the Java-provided Optional types. Please note that the @PathParam is an exception for which Optional is not available. The reason is that Optional for the @PathParam use case would just be a NO-OP, since an element of the path cannot be omitted.