I want to build a simple endpoint that returns an Order
object where I can search for this order by a single query parameter or a combination of several query parameters altogether. All of these query parameters are optional and the reason is that different people will access these orders based on the different Ids.
So for example:
/order/items?itemId={itemId}&orderId={orderId}&deliveryId={deliveryId}&packetId={packetId}
@GetMapping(path = "/order/items", produces = "application/json")
public Order getOrders(@RequestParam Optional<String> itemId,
@RequestParam Optional<String> orderId,
@RequestParam Optional<String> deliveryId,
@RequestParam Optional<String> packetId) { }
I could of course also skip the Java Optional and use @RequestParam(required = false)
, but the question here is rather how do I escape the if-else
or .isPresent()
nightmare of checking whether the query params are null? Or is there an elegant way, depending on the constellation of params, to pass further to my service and Spring Data JPA repository.