I'm building an API in Spring and I have a quick question:
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?
@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:
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. According to @mbridges in comment, I also strongly recommend to use array instead of listing all parameters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.