簡體   English   中英

Json對象解析並傳遞給Spring Contoller

[英]Json object parse and pass to spring contoller

我的jsp文件中有一個文件上傳組件,並且我僅接受json格式的文件。 我想獲取json文件並將其傳遞給spring並在spring中對json對象進行操作。

我的JavaScript看起來像下面

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類

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

同樣,當我上傳文件時,它會到達控制器類,但是在瀏覽器控制台中,它將拋出錯誤

“找不到網絡錯誤:404-http:// localhost:8080 / proj / fileUpload”

但是當我嘗試從JSON.stringify(hardcoded)打印硬編碼的值時

Json文件內容:

{'Gyr-x':10.11,'Gyr-y':9.66,'Gyr-z':10.93,'Temparature':30,'Pressure':101,'Humidity':15,'deviceId':1}
@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!");
}

並在javascript中嘗試url:fileUpload

您的頁面沒有命中控制器,這就是為什么它給404錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM