Presence of request param should evaluate to true

Robin-Hoodie :

Say I have an endpoint that accepts requests as follows:

GET https://my.website.com/products?expired

OR

GET https://my.website.com/products

The method I would expect to work:

@GetMapping
public List<Product> products(@RequestParam(value = "expired", required=false) boolean expired) {
   //Implementation details
}

This however, will return a Bad Request 400 response.

I know I would get this to work by sending the expired requestParam as expired=true, but I'd like for this to work similar to HTML boolean attributes where the mere presence of a request param represents true and its absence represents false

dbreaux :

I wonder if you'll have to implement two methods, one with and one without the param, the second with it required (and probably Boolean non-primitive, as the other answer suggests).

(Then just call a common method from both.)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=128510&siteId=1