java springmvc@PathVariable个别参数可能为空处理方法

问:假如id为非必需参数,可以为空,应该怎么处理

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

可以指定多个匹配路径

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

然后设置参数非必须

@PathVariable(required = falseString id

示例

@ApiOperation("获取订单接口")
  @ApiImplicitParams({
          @ApiImplicitParam(name = "pin", paramType = "path", dataType = "String", value = "账号", required = true),
          @ApiImplicitParam(name = "phone", paramType = "path", dataType = "String", value = "电话号码", required = false)
  })
  @ApiResponses({@ApiResponse(code = 501, message = "自定义异常xxx"), @ApiResponse(code = 500, message = "500", response = Errors.class)})
  @GetMapping(value = {"/list/{pin}/{phone}", "/list/{pin}"})
  public List getOrderList(@PathVariable String pin, @PathVariable(required = false) String phone) {
  }

你可能感兴趣的:(Java)