I have a form where I am submitting data with images.But when I click on submit button then I get the following error:
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'employee' on field 'fileData': rejected value [org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile@25d17d23]; codes [typeMismatch.employee.fileData,typeMismatch.fileData,typeMismatch.org.springframework.web.multipart.commons.CommonsMultipartFile,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.fileData,fileData]; arguments []; default message [fileData]]; default message [Failed to convert property value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile' for property 'fileData'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile' for property 'fileData': no matching editors or conversion strategy found]
I am using spring boot and I did the following:
application.properties
# multipart multipart.enabled=true spring.http.multipart.max-file-size=500000KB spring.http.multipart.max-request-size=500000KB My GET and POST methods are handled as follows:
@GetMapping("/add-employee") public ModelAndView empAdmin(Model model) { Employee employee=new Employee(); model.addAttribute("employee", employee); return new ModelAndView("add-new-employee"); } // POST: Save product @RequestMapping(value = { "/add-employee" }, method = RequestMethod.POST) public String productSave(@ModelAttribute("employee") Employee employee) { employeeService.saveEmployee(employee); return "redirect:/add-employee"; } My service class is:
import com.ashwin.vemployee.model.Employee; import com.ashwin.vemployee.repository.EmployeeRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class EmployeeService { @Autowired private EmployeeRepository empRepository; public Employee saveEmployee(Employee employee){ byte[] image = employee.getFileData().getBytes(); if (image != null && image.length > 0) { employee.setImage(image); } if(employee.getiNumber()==null){ empRepository.save(employee); } else{ empRepository.save(employee); } return employee; } } My add-new-employee.jsp looks like this:
<form:form modelAttribute="employee" method="POST" enctype="multipart/form-data"> <label>iNumber</label> <form:input path="iNumber" id="iNumber" type="text" class="form-control" required="required" /> <label>Full Name:</label> <form:input path="fullName" id="fullName" type="text" class="form-control" required="required" /> <label>Joined Date</label> <form:input path="joinedDate" id="joinedDate" type="text" class="form-control" required="required" /> <label>Position</label> <form:input path="position" id="position" type="text" class="form-control" required="required" /> <label>Reports To</label> <form:input path="reportsTo" id="reportsTo" type="text" class="form-control" required="required" /> <label>Cubicle No</label> <form:input path="cubicleNo" id="cubicleNo" type="text" class="form-control" required="required" /> <label>Job type</label> <form:input path="jobType" id="jobType" type="text" class="form-control" required="required" /> <td>Upload Image</td> <td> <form:input type="file" path="fileData" /> </td> <td> </td> <input type="submit" value="Submit" /> <input type="reset" value="Reset" /> </form:form> For handling the upload file option I added <form:input type="file" path="fileData" />. My model class looks like this Employee.java.
import org.springframework.web.multipart.commons.CommonsMultipartFile; import javax.persistence.*; import javax.validation.constraints.NotBlank; import java.util.Date; @Entity @Table(name = "employee") public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; @NotBlank private String iNumber; @NotBlank private String fullName; // @NotBlank private String joinedDate; @NotBlank private String position; @NotBlank private String reportsTo; @NotBlank private String cubicleNo; @NotBlank private String jobType; @Lob @Column(name = "Image", length = Integer.MAX_VALUE, nullable = true) private byte[] image; // Upload file. //@Transient private CommonsMultipartFile fileData; //all default construcotr and getters and setters I added new dependecy in my pom.xml.
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.1</version> </dependency> My whole pom.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.ashwin</groupId> <artifactId>vemployee</artifactId> <version>0.0.1-SNAPSHOT</version> <name>vemployee</name> <description>Demo project for Spring Boot for offc</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!-- https://mvnrepository.com/artifact/jstl/jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- needed for jsp --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <version>9.0.27</version> </dependency> <!--bootsrap and jquery--> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>4.3.1</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.4.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.webjars/bootstrap-datepicker --> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap-datepicker</artifactId> <version>1.7.1</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> Why I am getting this error?