0

I'm using ReactJS frontend with SpringBoot and I get the above error. The root cause for this error is the MultiPartFile in DTO that I'm using to get data from the post request to the backend.

@Getter
@Setter
public class ItemDTO {
    private long craftId;

    private MultipartFile imgFile;

    private int itemQuantity;

    private String shortDescription;

    private String longDescription;
}

The nested exception:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of org.springframework.web.multipart.MultipartFile (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information

code from the Reactjs frontend with: import {Button, Form, FormGroup, Label, Input,FormText} from 'reactstrap';

<FormGroup>
     <Label for="exampleFile">File</Label>
     <Input type="file" name="file" onChange={this.fileChangedHandler} id="exampleFile" />
</FormGroup>
fileChangedHandler = (event) => {
    this.setState({ image: event.target.files[0] })

}

the console.log of the payload:

image: File
name: "1605baeba66180d5337f41d9c80617a9.jpg"
lastModified: 1578835072895
lastModifiedDate: Sun Jan 12 2020 18:47:52 GMT+0530 (India Standard Time) {}
webkitRelativePath: ""
size: 45002
type: "image/jpeg"
__proto__: File

Any help would be appreciated.

Mandar Dharurkar
  • 267
  • 1
  • 5
  • 16
Dilrukshi Perera
  • 917
  • 3
  • 17
  • 31

1 Answers1

0

The first check try with the latest version.

Solution 1.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.4</version>
</dependency>

Solution 2.

  1. Set spring.jackson.serialization.FAIL_ON_EMPTY_BEANS=false in application.properties

    or

    1. @Bean public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(mapper); return converter; }

Atul Jain
  • 1,035
  • 2
  • 16
  • 24