nicearma
nicearma

Reputation: 780

Java jackson with MultipartFile

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.

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.

This is my 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....

}

and this 2 objects will be in

public class JsonTest {

    private JsonOtherInformation info;

    private JsonDocFile doc[];

    set... get...

}

and my service spring is

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

Upvotes: 5

Views: 5515

Answers (1)

Michał Ziober
Michał Ziober

Reputation: 38655

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).

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.

Upvotes: 1

Related Questions