I would like to ask, can you specify the default value when @ PathVariable is empty?

@RequestMapping(value = "/get/{id}/{userId}", method = RequestMethod.GET)
    public Result getMemberShip(@PathVariable("id") int id,@PathVariable("userId") int userId) {

if id is a non-required parameter, it can be empty. How should it be handled? you can give it a default value, or not in this way, can java give the default value of the passed parameter? it is not clear at the beginning to write java,

.
Mar.31,2021

multiple matching paths can be specified

@RequestMapping(value = {"/get/{userId}", "/get/{id}/{userId}"}, method = RequestMethod.GET)

then set the parameter is not required

@PathVariable(required = false) String id

@ PathVariable cannot set the default value because null is meaningless to url.
if the parameter may indeed be empty and you need a default value, you can use @ RequestParams.


{id} / {userId} if they are not necessary, do not pass parameters like this, use @ ModelAttribute annotation and pass parameters with object


The article you have shared here very awesome. I really like and appreciated your work. I read deeply your article, the points you have mentioned in this article are useful

happy wheels new

Menu