问:假如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 = false) String 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) {
}