gpt4 book ai didi

java - Spring中如何迭代@RequestParam?

转载 作者:行者123 更新时间:2023-11-30 06:57:36 26 4
gpt4 key购买 nike

我正在 Spring 中构建一个 API,我有一个简单的问题:

我想在对下面的这些参数执行一些逻辑之前检查它们是否包含值。

我是 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)

最佳答案

如果您想对所有这些值执行验证并在结构中关闭它,我建议创建 DTO 对象并将所有变量作为字段,例如:

public class FilesDto{
private MultipartFile video;
private MultipartFile photos[];
}
//Getters setters...

然后在你的 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);
}

其中someValidator是实现Validator接口(interface)的类。根据评论中的@mbridges,我还强烈建议使用数组而不是列出所有参数。

关于java - Spring中如何迭代@RequestParam?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41515029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com