[英]How to iterate though @RequestParam in Spring?
I'm building an API in Spring and I have a quick question:我正在 Spring 中构建 API,我有一个简单的问题:
I want to perform checks on these parameters below to see whether they contain values, before carrying out some logic on them.在对它们执行一些逻辑之前,我想对下面的这些参数执行检查以查看它们是否包含值。
I'm new to Spring - is there anyway to do "get" out these values into some sort of data structure so I can perform my checks?我是 Spring 的新手 - 有没有办法将这些值“获取”到某种数据结构中,以便我可以执行检查?
@RequestParam(value = "video") MultipartFile video,
@RequestParam(value = "pictureOne") MultipartFile pictureOne,
@RequestParam(value = "pictureTwo") MultipartFile pictureTwo,
@RequestParam(value = "pictureThree") MultipartFile pictureThree,
@RequestParam(value = "pictureFour", required = false) MultipartFile pictureFour,
@RequestParam(value = "pictureFive", required = false) MultipartFile pictureFive,
@RequestParam(value = "pictureSix", required = false) MultipartFile pictureSix,
@RequestParam(value = "pictureSeven", required = false) MultipartFile pictureSeven,
@RequestParam(value = "pictureEight", required = false) MultipartFile pictureEight,
@RequestParam(value = "pictureNine", required = false) MultipartFile pictureNine,
@RequestParam(value = "pictureTen", required = false) MultipartFile pictureTen)
If you want to perform validation for all this values and close it in structure I recommend to create DTO object and put all variables as fields, ex:如果要对所有这些值执行验证并在结构中关闭它,我建议创建 DTO 对象并将所有变量作为字段,例如:
public class FilesDto{
private MultipartFile video;
private MultipartFile photos[];
}
//Getters setters...
Then in your Controller然后在你的控制器中
@Autowired
private SomeValidator someValidator;
@RequestMapping(method = RequestMethod.POST)
public void savePhotos(@Valid FilesDto filesDto) {
....
}
@InitBinder("filesDto")
protected void initBinde(WebDataBinder binder) {
binder.setValidator(someValidator);
}
Where someValidator
is class that implements Validator
interface.其中
someValidator
是实现Validator
接口的类。 According to @mbridges in comment, I also strongly recommend to use array instead of listing all parameters.根据@mbridges 在评论中的说法,我还强烈建议使用数组而不是列出所有参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.