Reputation: 7665
I would like to upload multiple JSON files (like student grades JSON, and student courses schedule JSON, student assignment JSON etc) and metadata (like student information)
To Rest service running on Jersy and tomcat
What is the approach to take here? should it be like a single controller? is it possible to specify the uploaded 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) {
Upvotes: 7
Views: 1827
Reputation: 5741
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:
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 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.
Upvotes: 2
Reputation: 456
@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();
}
}
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]);
}
});
};
Upvotes: 3