1 Replies - 435 Views - Last Post: 26 September 2019 - 10:12 AM

#1 alagavat   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 14-March 19

Cannot insert data as image(file upload) and date in Spring Boot via T

Posted 11 September 2019 - 12:05 AM

I have done some couple of exercise about Spring Boot with Thmeleaf as CRUD operation including uploading file , register date except for other things but there is a typo. I couldn't insert information in the database because of not uploading file and register date.When I click save button as insertion process, it throws an error.

Can you check it if you don't mind. I'll appreciate you if you can help me solve it out.

How can I fix it?

Here is the error shown below.

Field error in object 'employee' on field 'registerDate': rejected value [2019-09-11]; codes [typeMismatch.employee.registerDate,typeMismatch.registerDate,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.registerDate,registerDate]; arguments []; default message [registerDate]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'registerDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.validation.constraints.NotNull @javax.persistence.Temporal @javax.persistence.Column java.util.Date] for value '2019-09-11'; nested exception is java.lang.IllegalArgumentException]
    Field error in object 'employee' on field 'image': rejected value [org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$[email protected]]; codes [typeMismatch.employee.image,typeMismatch.image,typeMismatch.[B,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [employee.image,image]; arguments []; default message [image]]; default message [Failed to convert property value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'byte[]' for property 'image'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile' to required type 'byte' for property 'image[0]': PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value of type 'org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile']]


Employee

@Entity
@Table(name="EMPLOYEE",catalog = "SPRINGDATABASE")
public class Employee implements Serializable{
 
    @Id
    @SequenceGenerator(name="EMPLOYEE_SEQ", sequenceName="EMPLOYEE_SEQ", allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="EMPLOYEE_SEQ")
    @Column(name="ID", nullable = false)
    private int id;
     
    @NotNull(message="is required")
    @Column(name="FIRSTNAME")
    private String firstName;
     
    @NotNull(message="is required")
    @Column(name="LASTNAME")
    private String lastName;
     
    @NotNull(message="is required")
    @Column(name="EMAIL")
    private String email;
     
    @NotNull(message="is required")
    @Column(name="IMAGE")
    private byte[] image;
     
    @NotNull(message="is required")
    @Temporal(TemporalType.DATE)
    @Column(name="REGISTER_DATE")
    private Date registerDate;
     
    @Transient
    private String base64Image;
 
        public int getId() {
        return id;
    }
 
 
    public void setId(int id) {
        this.id = id;
    }
 
 
    public String getFirstName() {
        return firstName;
    }
 
 
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
 
 
    public String getLastName() {
        return lastName;
    }
 
 
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
 
 
    public String getEmail() {
        return email;
    }
 
 
    public void setEmail(String email) {
        this.email = email;
    }
 
 
    public byte[] getImage() {
        return image;
    }
 
 
    public void setImage(byte[] image) {
        this.image = image;
    }
 
 
    public Date getRegisterDate() {
        return registerDate;
    }
 
 
    public void setRegisterDate(Date registerDate) {
        this.registerDate = registerDate;
    }
 
 
    public String getBase64Image() {
        base64Image = Base64.getEncoder().encodeToString(this.image);
        return base64Image;
    }
 
 
    public void setBase64Image(String base64Image) {
        this.base64Image = base64Image;
    }
 
}






Thymleaf Part

<form action="#" th:action="@{/employees/save}"
    						 th:object="${employee}" method="POST" enctype="multipart/form-data">
    
    <input type="file" name="image" th:field="*{image}"
    					class="form-control mb-4 col-4" placeholder="Image">	
    					
    			<input type="date" name="date" th:field="*{registerDate}"
    					class="form-control mb-4 col-4" placeholder="Register Date">			
    				
    			<button type="submit" class="btn btn-info col-2">Save</button>
    						
    		</form>



Controller

@PostMapping("/save")
    	public String saveEmployee(@ModelAttribute("employee") Employee theEmployee
    			,@RequestParam("file") MultipartFile file,
    			@RequestParam("date") String date) throws IOException, ParseException {
    		
    		// Image
    		System.out.println("/save | File Name : "+file.getName());
            byte[] imageBytes = file.getBytes();
     
            try {
                FileInputStream fileInputStream = new FileInputStream(file.getOriginalFilename());
                fileInputStream.read(imageBytes);
                fileInputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            theEmployee.setImage(imageBytes);
            
            // Date
            SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
    		Date registerDate = dateformat.parse(String.valueOf(date));
    		
    		System.out.println("/save | registerDate : " + registerDate);
    		
    		theEmployee.setRegisterDate(registerDate); 
    		
    		// save the employee
    		employeeService.save(theEmployee);
    		
    		// use a redirect to prevent duplicate submissions
    		return "redirect:/employees/list";
    	}


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot insert data as image(file upload) and date in Spring Boot via T

#2 DBonwick   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-September 19

Re: Cannot insert data as image(file upload) and date in Spring Boot via T

Posted 26 September 2019 - 10:12 AM

Did you check your program code? I donít see any mistake here or a bug.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1