How to upload multiple json files and generate data for a leisure service

I would like to upload several JSON files (e.g. student JSON grades and JSON student course schedules, JSON student assignments, etc.) and metadata (e.g. student information)
For service running on Jersy and tomcat

What will be the approach? should it be one controller? can I specify a loaded JSOn structure? What if one of the files is missing?

@Path("/submitStudentInformation") public class SubmitStudInfoController { @POST @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces("text/plain") @Path("/multipleFiles") public Response uploadFiles(@Context HttpServletRequest request) { 
+7
json rest
source share
2 answers

Send a list of files in rest api mode

 @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(); } } 

The front end I use angularjs, so I install several files in formdata h2>

 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]); } }); }; 
+3
source share

First you must define entities for yourself or technically / simply (but not exactly) tables to store your data.

I assume it will be something like:

  • students - a table for students
  • grades - student grades here Courses
  • - there will be a course schedule.

Further, the idea of ​​REST is that the record is managed (updated / inserted / deleted) individually. Data is transferred as raw JSON in the content body for a particular record. However, you can also process multiple entries in a batch. For example, if on an insert (POST method) you pass an array of JSON and not an object, that is, you send several records, then you make several attachments to the end: inserting a student will take something like this: {"name": "John"} , but the insertion of several students will look something like this: [{"name": "John"}, {"name": "Davy"}]

Usually in REST, you do not need to upload JSON files yourself, you pass the data as JSON to the service. Think twice if you really need to load data into JSON as files. However, this is technically possible. In the case of file downloads, you need to transfer the data in the form, and not in the form of source code, as the classic REST approach.

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

  • GET - a list of all recordsets or individual (/ students / 5 /)
  • POST - insert a new entry with JSON from the body
  • PUT - updating a specific record, usually with an identifier, for example (/ students / 5 /)
  • DELETE - delete a record from a set of records, usually with an identifier, for example (/ students / 5 /)

GETs can be improved with filtering, swapping, etc. And, of course, you have to take care of security, data access / management level management.

For operations targeting individual write operations, such as editing / deleting, the record identifier can be passed in part of the URI or as a parameter or in the content body. You decide.

+2
source share

All Articles