0

I have a file upload component in my jsp, and i accepts only json format files. i want to get json file and pass it to spring and do manipulations of the json object in spring.

my javascript looks like below

var file = this.files[0];
 var reader = new FileReader();

  var oMyForm = new FormData();
  oMyForm.append("file", this.files[0]);

 console.log('reader='+JSON.stringify(reader)) // this does not print any it prints empty as "reader={}"
console.log('oMyForm ='+JSON.stringify(oMyForm )) // this does not print any it prints empty as "oMyForm={}"
var urlpath =  "<%=request.getContextPath()%>/fileUpload";
  $.ajax({
                    data : JSON.stringify(reader),
                    type : 'POST',
                    dataType: 'json',
                    headers: { 
                        'Accept': 'application/json',
                        'Content-Type': 'application/json' 
                    },
                    url : urlpath,
                    success : function(data){
                    }
               });

Java class

@RequestMapping(value= "/fileUpload", method=RequestMethod.POST,consumes="application/json")
    public void fileUpload(){
         //here i want to get the json object and do parsing and manipulations on the object
         System.out.println(" uploaded!");
    }

As well, when i upload the file it hits the controller class, but in browser console it throws error as

"NetworkError: 404 Not Found - http://localhost:8080/proj/fileUpload"

But when i try to print hardcoded value from JSON.stringify(hardcoded)

Json file content:

{'Gyr-x':10.11,'Gyr-y':9.66,'Gyr-z':10.93,'Temparature':30,'Pressure':101,'Humidity':15,'deviceId':1}
user2000189
  • 479
  • 4
  • 6
  • 22

1 Answers1

0
@RequestMapping(value= "/fileUpload", method=RequestMethod.POST,consumes="application/json")
public void fileUpload(@requestBody String json){
     //here i want to get the json object and do parsing and manipulations on the object
     System.out.println(" uploaded!");
}

and in javascript try url:fileUpload

your page is not hitting controller thats why its giving 404 error