Deserialization of File type from Angular to Java using Jackson

Viewed 2003

In my REST application developed using SpringBoot and Angular, I need to send a REST request to SpringBoot back end in order to save a object into the database.

In the front end, user fills up a form and submits. Following is the interface from which a object is created in order to be sent to the backend as a REST call.

export interface TenderToPost{

    id: number;
    name: string;
    description: string;
    images: File[];
    referenceNumber: string;

}

The backend should deserialize the above object to a object of Following class in Java.

public class TenderDTO implements Serializable{

    private long id;

    private String name;

    private String description;

    private List<MultipartFile> images;

    private String referenceNumber;

    // default constructor

    // geters and setters

}

As shown above, I need to accept the File sent from frontend as a MultipartFile.

However, when the request is sent from the front end, Jackson gives me the error

Can not construct instance of org.springframework.web.multipart.MultipartFile: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information

I am aware that that this error comes since I have used MultipartFile interface as the type of my images list.

What I thought of as the solution for this, is letting Jackson know to what type the list should be deserialized into using an annotation. So, I used following

@JsonDeserialize(contentAs = CommonsMultipartFile.class)
private List<MultipartFile> images;

CommonsMultipartFile is an implementation of MultipartFile. That is why I used it.

Now I get the following error.

Can not construct instance of org.springframework.web.multipart.commons.CommonsMultipartFile: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)

Do I need to change the setter for the images list? Or is my whole approach incorrect?

Kindly show me the way how I should correct this.

1 Answers
Related