I have a controller method like so
@RequestMapping(value = "/update", method = RequestMethod.POST)
RestResponse updateId(@RequestParam(value = "cityId") Optional<Integer> cityId) {
}
Now when I send cityId
without a value, cityId.isPresent()
returns false, which is wrong because I actually included cityId
in my request parameters but I just didn't set a value.
I noticed this behavior doesn't happen on Optional<String>
, it actually tells me that the parameter is present even if it doesn't have a value.
So how should I handle parameters the likes of Optional<Integer>
? I just need to determine that the parameter was sent even if it had no value (because it's optional and if it was sent without a value I have to update the database)
edit: Looks like what I wrote above is confusing, I'll try to describe the issue again
@RequestMapping(value = "/temp", method = RequestMethod.POST)
void temporary(@RequestParam(value = "homeCityId") Optional<Integer> homeCityId) {
if(homeCityId.isPresent()) {
System.out.println("homeCityId is Present");
} else {
System.out.println("homeCityId is Not Present");
}
}
I make a request that includes homeCityId
with an empty value, I get homeCityId is Not Present
. How can I distinguish between a request that have homeCityId
with an empty value and a request that didn't include homeCityId
at all?