How to upload multiple json files and form data to rest service How to upload multiple json files and form data to rest service json json

How to upload multiple json files and form data to rest service


Send the files list in rest api

@POST@Path("/uploadFile") @Consumes(MediaType.MULTIPART_FORM_DATA)@Produces(MediaType.APPLICATION_JSON)public Response uploadFile(@FormDataParam("files") List<FormDataBodyPart> files) if(files!=null) {     for (int i = 0; i < files.size(); i++) {          FormDataBodyPart this_formDataBodyPartFile = files.get(i);          ContentDisposition this_contentDispositionHeader = this_formDataBodyPartFile.getContentDisposition();          InputStream this_fileInputStream = this_formDataBodyPartFile.getValueAs(InputStream.class);          FormDataContentDisposition fileDetail = (FormDataContentDisposition) this_contentDispositionHeader;          String imagename = fileDetail.getFileName();     }} 

Front end i am using angularjs so i set multiple files in formdata

var formdata = new FormData();$scope.getTheFiles = function(element) {    $scope.$apply(function($scope) {        $scope.files = element.files;        for (var i = 0; i < element.files.length; i++) {            formdata.append('files', element.files[i]);        }    });};


First, you should define for yourself the entities or technically/simple said (but not exactly) tables to store your data.

I assume it will be something like:

  • students - table to store students
  • grades - student grades here
  • courses - here will stay courses schedule

Next, the idea of the REST is that record is managed (updated/inserted/deleted) individually. Data is passed as raw JSON in content body for a specific record. However, you can process also multiple records in a batch. For example, if on insert (POST method) you pass a JSON array, not an object, meaning you send multiple records, then you do multiple inserts on back-end: inserting a student will take something like: {"name": "John"}, but inserting of multiple students will be something like: [{"name": "John"}, {"name": "Davy"}]

Usually, in REST you don't have to upload JSON files itself, you pass the data as JSON to the service. Think twice if you really need to upload data in JSON as files. However this is technically possible. In case of file uploading, you need to pass the data as form-encoded, not raw, as a classical REST approach.

Later define a URI for each entity, for example for the students it will be something like /api/students/[id/] with the REST style functionality based on HTTP method:

  • GET - list entire record set or an individual one (/students/5/)
  • POST - insert a new record with the JSON from the body
  • PUT - update the specific record, usually with id, like (/students/5/)
  • DELETE - delete a record from the record set, usually with id, like (/students/5/)

GET can be improved with filtering feature, paging, etc... And of course, you should care about security, data access/manage control layer.

For operation targeting single record operations like edit/delete, the record identifier can be passed in the URI part or as a parameter or in content body. You decide.