首页 > 解决方案 > Spring MVC @PathVariable 列表输入 javax 最大大小验证

问题描述

Spring Boot-Rest API 下的 Spring MVC 项目

@GetMapping(value = "bulk/{speakerIds}")
@ResponseStatus(HttpStatus.MULTI_STATUS)
@Operation(summary = "Gets Speakers to a max Limit of 10")
public Map<Long, Object> getMasterClassBulk(@PathVariable List<Long> speakerIds) {

    return speakerHandler.getSpeakers(speakerIds);

}

有没有办法添加 List Max Size 验证?eg:当输入列表超过10大小时,抛出404-Bad Request

尝试了下面的代码,但它不起作用。

@PathVariable @Size(max=10) List<Long> speakerIds

标签: javaspringspring-bootspring-mvc

解决方案


尝试使用 @Validated 注释类并在方法参数上尝试:

@Valid @PathVariable @Size(max=10) List<Long> speakerIds

推荐阅读