search

Home  >  Q&A  >  body text

java - I would like to ask, can a default value be specified 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 and can be empty, how should it be handled? It can be given a default value, or it should not be used in this way. Can java give default values ​​to the passed parameters? I just started writing java, so it is not very easy. clear

巴扎黑巴扎黑2784 days ago1289

reply all(2)I'll reply

  • 習慣沉默

    習慣沉默2017-05-17 10:08:50

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

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-17 10:08:50

    {id}/{userId} If they are not necessary conditions, do not pass parameters like this. It is better to use @ModelAttribute annotation and use objects to pass parameters

    reply
    0
  • Cancelreply