Java Jackson与MultipartFile [英] Java jackson with MultipartFile

查看:106
本文介绍了Java Jackson与MultipartFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,首先,我将使用Spring 4来解释我在做什么,我必须做一个应用程序来发送一些带有一些附件[PDF]的信息,并且每个文件都有一个标题,参考等.因此,我将对象JSONJackson 2.3.3混合在一起.

Well, first I will explain what I'm doing, I using Spring 4, and I have to do one application for send some information with some attachments [PDF] and each one this files have one title, reference, etc. So, I made some mixup of objects JSON with Jackson 2.3.3.

因此,在提出代码之前,我想问一下,我可以将MultipartFileJackson(或可能是File)一起使用吗? 在网络上看到很多关于文件和JSON的答案和信息之后,我不清楚是否可以从客户端使用JSON发送文件.而且,如果可以的话,我是否可以在JSON对象中发送此文件,并使其具有一个包含对象数组的属性,每个对象将包含字符串类型的属性,而最后一个将是文件.

So, before to put my code, I want to ask, can I use MultipartFile with Jackson (or maybe File)? After I saw a lots of answers and information in the web about files and JSON, I am not clear if I can send files with JSON from the client side. And if it is possible, can I send this file in JSON object with one property that contain arrays of objects, each object will contain properties type string and the last one will be the file.

这是我的POJO:

public class JsonDocBase{

    protected String tp="invoice";

    protected String cmt;

    protected String title;

    protected String lang="FR";

    protected String ref;

    protected MultipartFile file;

    -----get.. and set....
}


public class JsonOtherInformation{    
    String nm;

    String cmt;

    String orgMsgId;

    String tp;

    get.... set....

}

,这2个对象将位于

public class JsonTest {

    private JsonOtherInformation info;

    private JsonDocFile doc[];

    set... get...

}

我的服务春天是

@RequestMapping(value = "/simple", method = RequestMethod.POST)
public MessageDb creatSimpleeActivationRequest(@RequestBody JsonTest jRequestSimple

推荐答案

我认为,在JSON中发送文件内容不是一个好主意.您可以使用JSON返回有关文件的信息,其中可能包含用于下载给定文件的URL. Google云端硬盘SDK 中使用了此方法. Files.get 用于检索文件元数据,以及是否要下载文件内容,您可以使用元数据对象中的 downloadUrl 属性. (另请参见 Drive SDK-下载文件.)

I think, that it is not a good idea to send file content in JSON. You can use JSON to return informations about file which could contain URL to download given file. This method is used in Google Drive SDK. Files.get is used to retrieve file metadata and if you want to download file content you can use downloadUrl property from metadata object. (See also Drive SDK - Download Files).

但是,如果您真的想在JSON中返回文件的内容,则必须将此内容转换为String.您可以使用 Base64 对内容进行编码,并且客户端必须在解析后对其进行解码.您可以编写自定义序列化程序,该序列化程序可以将MultipartFile对象转换为字符串.

But if you really want to return file's content in JSON you have to convert this content into String. You can use Base64 to encode content and client has to decode it after parsing. You can write custom serializer which can convert MultipartFile object into string.

这篇关于Java Jackson与MultipartFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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