I have a Spring Boot controller method that handles file uploads, and I'm trying to use the @ModelAttribute annotation to bind some fields from the request. Here's my controller method:
@PostMapping(value = "/{accountNo}/upload", consumes = "multipart/form-data") public ResponseEntity<Object> uploadDocument( @PathVariable Long accountNo, @ModelAttribute RequestDTO requestDTO, @RequestParam("file") MultipartFile file, Authentication authentication) { // ... } The RequestDTO class contains fields that I want to bind from the request body/form data. However, I'm encountering an issue where the requestDTO object is always null in the method.
Due to the extensive length of certain attribute values, passing DTO data through request parameters is not feasible.
Is there something I'm missing in my setup, or are there any common pitfalls when using @ModelAttribute in this context? How can I ensure that the requestDTO object is correctly populated from the request?
I have verified that the request is being sent correctly, including the required fields for RequestDTO.
The @RequestBody annotation cannot be used in conjunction with multipart/form-data.
I used swagger to send data using JSON format.
{ "accountNo": "211100001201", "kind": "hello", "name": "string", "url": "string", "location": "string", "storageStatus": "string", "actionType": "string", "value1": "string", "detail": "string" } Thank you for your assistance!
null, the fields can benull, if that is the case there is something that youa re using that isn't in the question (likefinalmethods or class, implementing an interface etc. etc.). Also what is the form you are sending? How are you sending.@ModelAttribute.