Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultipartFile is Null in @ModelAttribute with Spring Boot 3.3.4 but Works in 3.2.4 #33791

Closed
kkong101 opened this issue Oct 25, 2024 · 8 comments
Assignees
Labels
for: external-project Needs a fix in external project in: web Issues in web modules (web, webmvc, webflux, websocket)

Comments

@kkong101
Copy link

kkong101 commented Oct 25, 2024

Title:
MultipartFile is Null in @ModelAttribute with Spring Boot 3.3.4 but Works in 3.2.4

Description:

When upgrading from Spring Boot version 3.2.4 to 3.3.4, the MultipartFile parameter in the controller method becomes null. This issue does not occur in version 3.2.4, where the file is correctly received. There are no restrictions on file size or other related configurations.

Steps to Reproduce

  1. Controller Setup:

    @PostMapping("/users")
    fun insertTryOnFace(
        @ModelAttribute imageFile: MultipartFile,
        @ModelAttribute gender: Gender,
    ) {
        // Implementation
    }
  2. Send a POST Request:

    • Endpoint: /users
    • Method: POST
    • Content-Type: multipart/form-data
    • Parameters:
      • imageFile: [Attach a file]
      • gender: [Provide appropriate value]
  3. Observe the Behavior:

    • Spring Boot 3.3.4: imageFile is null.
    • Spring Boot 3.2.4: imageFile is correctly received.

Expected Behavior
The MultipartFile should be correctly populated with the uploaded file data regardless of the Spring Boot version, provided there are no size or configuration restrictions.

Actual Behavior
In Spring Boot 3.3.4, the imageFile parameter is null when handling the POST request, whereas it works as expected in 3.2.4.

Additional Information

  • Spring Boot Versions:
    • Affected: 3.3.4
    • Working: 3.2.4
  • File Size Limits: No restrictions set; default configurations are used.
  • Environment: �MacOs
  • Relevant Dependencies: jvmTarget 17, kotlinVersion 2.0.21, io.spring.dependency-management 1.1.6

Request
Looking for assistance or a fix to ensure that MultipartFile is correctly populated in Spring Boot 3.3.4 when using @ModelAttribute.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 25, 2024
@bclozel bclozel transferred this issue from spring-projects/spring-boot Oct 25, 2024
@bclozel bclozel added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Oct 25, 2024
@kkong101
Copy link
Author

move to spring-boot repository

@bclozel
Copy link
Member

bclozel commented Oct 25, 2024

@kkong101 at a quick glance, I don't think this is due to a behavior change in Spring Boot but probably in Spring Framework. Please let us know assess the situation and we'll move the issue back to Spring Boot if necessary. This issue was moved to Spring Framework on purpose.

@bclozel
Copy link
Member

bclozel commented Oct 28, 2024

I couldn't reproduce this problem with Spring Boot 3.3.5.

I've used the following controller:

package org.example.formfile;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class FormController {

	private static final Log logger = LogFactory.getLog(FormController.class);

	@PostMapping(path = "/users", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	@ResponseBody
	public String insertTryOnFace(@ModelAttribute MultipartFile imageFile) {
		logger.info("file name: " + imageFile.getOriginalFilename());
		logger.info("file size: " + imageFile.getSize());

		return imageFile.getOriginalFilename();
	}

}

and issued the following request:

curl -v -F imageFile=@file.txt http://localhost:8080/users
* Host localhost:8080 was resolved.
* IPv6: ::1
* IPv4: 127.0.0.1
*   Trying [::1]:8080...
* Connected to localhost (::1) port 8080
> POST /users HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/8.7.1
> Accept: */*
> Content-Length: 230
> Content-Type: multipart/form-data; boundary=------------------------o0UOAd4nXSat6Fym1GH9Od
>
* upload completely sent off: 230 bytes
< HTTP/1.1 200
< Content-Type: text/plain;charset=UTF-8
< Content-Length: 8
< Date: Mon, 28 Oct 2024 13:02:56 GMT
<
* Connection #0 to host localhost left intact
file.txt% 

There's probably something else going on, so I'm closing this issue for now. We can reopen if you can provide a minimal sample application.

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale Oct 28, 2024
@bclozel bclozel added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 28, 2024
@EnyaEnya
Copy link

EnyaEnya commented Nov 14, 2024

There's probably something else going on, so I'm closing this issue for now. We can reopen if you can provide a minimal sample application.

@bclozel I have the same problem, here is a sample

https://github.com/EnyaEnya/multipart-null

@Panfloeter
Copy link

@bclozel We experience the same problem after upgrading from Spring Boot 3.3.1 to 3.4.2 and don't quite understand why this issue was closed. With the sample of @EnyaEnya at hand, could you re-analyze the problem and provide a fix and/or workaround? Thanks a lot!

@bclozel bclozel reopened this Feb 12, 2025
@bclozel bclozel added status: waiting-for-triage An issue we've not yet triaged or decided on and removed status: invalid An issue that we don't feel is valid labels Feb 12, 2025
@alindl
Copy link

alindl commented Feb 18, 2025

I had the same issue, I just used @RequestParam("file") instead of @ModelAttribute("file").
That's at least how it's suggested in their own guide: https://spring.io/guides/gs/uploading-files

@kkong101
Copy link
Author

kkong101 commented Feb 18, 2025

I solved it by using @RequestPart("file") instead of @ModelAttribute("file"). Switching to @RequestPart allowed Spring Boot 3.3.4 to correctly bind the multipart file from the request, and everything worked as expected.

@sdeleuze sdeleuze self-assigned this Feb 19, 2025
@sdeleuze
Copy link
Contributor

After a deeper look, seems to be triggered by adding org.springframework.data:spring-data-commons:3.3.4 dependency, so I will close this issue in favor of spring-projects/spring-data-commons#3258.

@sdeleuze sdeleuze closed this as not planned Won't fix, can't repro, duplicate, stale Mar 20, 2025
@sdeleuze sdeleuze added for: external-project Needs a fix in external project and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project Needs a fix in external project in: web Issues in web modules (web, webmvc, webflux, websocket)
Projects
None yet
Development

No branches or pull requests

7 participants