Skip to content

DataBinder fails with "No primary or single unique constructor found for interface MultipartFile" #31669

@mschneid

Description

@mschneid

The following example works with Spring Framework 6.0, but not with 6.1:

class MyForm {
	private String name;
	private MultipartFile file;

	MyForm(String name, @Nullable MultipartFile file) {
		this.name = name;
		this.file = file;
	}

	// ...
}

@Controller
public class FileUploadController {
	@PostMapping("/form")
	public String handleFormUpload(MyForm form, BindingResult errors) {
		if (!form.getFile().isEmpty()) {
			byte[] bytes = form.getFile().getBytes();
			// store the bytes somewhere
			return "redirect:uploadSuccess";
		}
		return "redirect:uploadFailure";
	}
}

The above code throws the exception java.lang.IllegalStateException: No primary or single unique constructor found for interface org.springframework.web.multipart.MultipartFile if the file is not specified in the request.

The method DataBinder#shouldCreateObject(MethodParameter) was introduced with #31488. I think this method should also check whether the parameter is optional or not by calling MethodParameter#isOptional().

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions