
1. Overview
In this article, we will discuss setting a default boolean value for Spring RequestParam. To know the difference between @RequestParam and @PathVariable, refer to this article.
2. Spring RequestParam
The?@RequestParam
?annotation binds the request parameters to a method parameter in your controller.
For example, the below getOrders
handler method of the Orders
controller handles all the incoming HTTP GET requests with path /orders
. The request param maps the query param since
to the method parameter sinceDate
.
@RestController public class OrdersController { Logger logger = (Logger) LoggerFactory.getLogger(OrdersController.class); @GetMapping("/orders") public String getOrders(@RequestParam(name = "since", required = false) String since) { logger.debug("Orders since " + since); return "Orders since: " + since; } }
When you hit the URI http://localhost:8080/orders?since=
2021-10-21, then the above getOrders
function filters the order created after the date Oct 21st, 2021.
By default, @RequestParam requires query parameters to be present in the URI. However, you can make it optional by setting?@RequestParam
‘s?required
?attribute to?false
. In the above example, the since
query param is optional:?@RequestParam(value="since", required=false)
).
The @RequestParam annotation supports the following parameters:
- defaultValue: Default value as a fallback mechanism when the request doesn’t have a value or is empty.
- name: Name of the query parameter to bind
- required: Whether the parameter is mandatory. By default, it is true.
- value: An alternative alias for the name attribute
3. RequestParam default value for Boolean object
As we mentioned earlier, @RequestParam
accepts the default value using the attribute defaultValue
. If the request URI doesn’t contain the query parameter or empty value passed, then the specified default value is used.
If you specify the defaultValue
, it implicitly sets the required
parameter to false.
For example, the following getOrderStatus
controller method gets the isCanceled
status as a boolean value. If the request URI doesn’t contain the query parameter isCanceled
or empty, then the Spring uses the defaultValue
false specified.
@RestController public class OrdersController { Logger logger = (Logger) LoggerFactory.getLogger(OrdersController.class); @GetMapping("/orderStatus") public String getOrderStatus(@RequestParam(name = "isCanceled", defaultValue = "false") Boolean isCanceled) { logger.debug("Order isCanceled " + isCanceled); return "Order isCancelled: " + isCanceled; } }
When you hit the URI http://localhost:8080/orderStatus
, then the above isCanceled
Boolean object contains the value false instead of the value null
.
If you execute the above code, the following result appears in the console.
Order isCanceled false
3.1. RequestParam value for primitive boolean
You can also use the primitive boolean
type for @RequestParam
in place of Boolean
. In this case, Spring automatically assigns false
.
For example, the following isCanceled
param is optional.
@GetMapping("/orderStatus") public String getOrderStatus(@RequestParam(name = "isCanceled", required = false) boolean isCanceled) { logger.debug("Order isCanceled " + isCanceled); return "Order isCancelled: " + isCanceled; }
When you hit the URI http://localhost:8080/orderStatus
, then the Spring assigns false
to the isCanceled
primitive.
If you execute the above code, the following result appears in the console.
Order isCanceled false
4. Conclusion
To sum up, we have learned to set a default value boolean for the Spring RequestParam. You can find the code samples of this article in our GitHub repository.
@RequestParam ( … defaultValue = false ) is wrong, it actually accepts strings only, so the right syntax is: defaultValue = “false”.
Apart from that, I’ve stumbled on this page, cause I’m trying to understand what happens if isCanceled is boolean (primitive type, not Boolean), defaultValue isn’t declared, required is false and the request hasn’t any isCanceled param: is isCanceled set to false? To true? Does it yield an error?
In other words, are there default values set by Spring, when defaultValue isn’t used in @RequestParam?
Thank you Marco for pointing it out.
I have updated the code.
To answer your question, if the
isCanceled
is a primitive boolean value and required false, then the request (http://localhost:8080/orderStatus)without isCanceled param yields false.
@GetMapping(“/orderStatus”)
public String getOrderStatus(@RequestParam(name = “isCanceled”,
required = false) boolean isCanceled) {
logger.debug(“Order isCanceled ” + isCanceled);
return “Order isCancelled: ” + isCanceled;
}