My project is a spring-boot project using the parent dependency and thymeleaf with some webjars dependencies for the UI. I don't what am I missing.
I cannot use MultipartFile as a field of my ModelAttributeDto.
The exception occurs when I want to open the HTML. My problem is I want to use the validation of thymeleaf, and do not want to use the MultipartFile as simple @RequestParam.
The details are here, if you wish the pom.xml I would happily share it.
Thank you for your help.
dependencies besides the standard spring-boot 2.4.3 dependencies, pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator</artifactId>
<version>0.40</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>font-awesome</artifactId>
<version>5.15.2</version>
</dependency>
@Controller
public class UploadController {
private final RestUploadService restUploadService;
@GetMapping("/upload")
public String load(Model model) {
model.addAttribute("uploadParams", new UploadParams());
return "upload/upload";
}
@PostMapping("/upload")
public String upload(
@Valid @ModelAttribute("uploadParams") UploadParams uploadParams,
BindingResult result,
Model model
) {
return "upload/upload";
}
}
UploadParams:
@Data
public class UploadParams {
String tableName;
MultipartFile fileSource;
}
The HTML form:
<form action="#" enctype="multipart/form-data" th:action="@{/upload}" th:object="${uploadParams}" method="post"
onsubmit="this.submit_button.disabled = true;">
<div class="form-group row">
<label for="tableName" class="col-sm-2 col-form-label">Table name</label>
<div class="col-sm-10">
<input id="tableName" class="form-control" type="text" th:field="*{tableName}"
placeholder="testExtendedTable"/>
<div class="alert alert-danger" th:if="${#fields.hasErrors('tableName')}" th:errors="*{tableName}"></div>
</div>
</div>
<div class="form-group row">
<label for="fileSource" class="col-sm-2 col-form-label">SDF file</label>
<div class="col-sm-10">
<input id="fileSource" name="fileSource" class="form-control-file" type="file" placeholder="testExtendedTable" th:field="*{fileSource}"/>
<div class="alert alert-danger" th:if="${#fields.hasErrors('fileSource')}" th:errors="*{fileSource}"></div>
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
The page can't even load, and I get the following exception:
org.springframework.beans.NotReadablePropertyException: Invalid property 'fileSource' of bean class [com.chemaxon.consultancy.codingexercise.adapters.api.dto.UploadParams]: Bean property 'fileSource' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:625) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:615) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:104) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:228) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:129) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) ~[thymeleaf-spring5-3.0.12.RELEASE.jar:3.0.12.RELEASE]
at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306) ~[thymeleaf-spring5-3.0.12.RELEASE.jar:3.0.12.RELEASE]
...