本文主要是介绍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 = 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) {}
这篇关于java springmvc@PathVariable个别参数可能为空处理方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!