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:

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.

The Optional types can also be used as type of the fields of a @BeanParam's class.

Here is an example of endpoint with a @BeanParam:

public String optDouble(@BeanParam Bean bean) {
    return Double.toString(bean.value.orElse(4242.0));

The corresponding class Bean:

public class Bean {
    OptionalDouble value;

Finally, the Optional types can be used directly as type of the fields of a Jakarta RESTful Web Services resource class.

Here is an example of a Jakarta RESTful Web Services resource class with an Optional type:

public class OptionalResource {
    Optional<String> value;