使用Jackson将文件类型从Angular反序列化为Java [英] Deserialization of File type from Angular to Java using Jackson

查看:182
本文介绍了使用Jackson将文件类型从Angular反序列化为Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我使用SpringBoot和Angular开发的REST应用程序中,我需要向SpringBoot后端发送REST请求,以便将对象保存到数据库中。

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.

在前端,用户填写表格并提交。以下是从中创建对象以将其作为REST调用发送到后端的接口。

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;

}

后端应将以上对象反序列化为Follow对象

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

}

如上所述,我需要接受前端发送的文件作为 MultipartFile

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


无法构造
的实例org.springframework.web.multipart.MultipartFile:抽象类型
需要映射为具体类型,具有自定义反序列化器,或者
包含其他类型信息

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

我知道此错误来自我使用过 MultipartFile 界面作为我的图片列表的类型。

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

我想到的解决方案是,让Jackson知道应使用反注释将列表反序列化为哪种类型。因此,我使用了以下

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是 MultipartFile 的实现。这就是为什么我使用它。

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

现在,我收到以下错误。

Now I get the following error.


无法构造
的实例org.springframework.web.multipart.commons.CommonsMultipartFile:找不到
合适的构造函数,无法从对象值
反序列化(缺少默认构造函数或创建者,或者也许是否需要添加/启用
类型信息?)

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.

推荐答案

尝试使用 @JsonPropertyDescription 构造对象。
@JsonPropertyDescription示例

Try to use @JsonPropertyDescription to construct object. @JsonPropertyDescription example

这篇关于使用Jackson将文件类型从Angular反序列化为Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆