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<OrderBaseVO> getOrderList(@PathVariable String pin, @PathVariable(required = false) String phone) {
  }

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值