我的开发环境:
Spring MVC 4.1.2
jackson 2.4.4
我正在尝试通过jQuery 1.7.2&a; ajax调用将复杂的JSON对象发送到我的spring MVC控件:
function uploadFileAjaxCall(fileid,filedata,filename,filetype,referid,remark){
var jsonData = {fileId:fileid,fileData: filedata, fileName: filename,fileType:filetype,referId:referid,fileRemark:remark}
jQuery.ajax({
url: "uploadFileByPartner",
processData:false,
type: 'GET',
data: jsonData,/*{json: JSON.stringify(jsonData)},*/
dataType: "json",
async: true,
beforeSend: function(xhr) {
xhr.setRequestHeader("Accept", "application/json");//"application/json");
xhr.setRequestHeader("Content-Type", "application/json");// "application/json");
},success:function(response){
document.getElementById("output").innerHTML=response;
},error:function(jqXhr, textStatus, errorThrown){
alert(textStatus);
}
});
};
JSON对象中有六个值,filedata字段是字节数组,大约是几百到几百字节。其他任何东西都很小。此函数用于将文件发送到服务器,而不使用表单提交。
我的POJO课程:
public class FileEntity implements serialisable{
private String fileId;
private byte[] fileData;
private String fileName;
private String fileType;
private String referId;
private String fileRemark;
set/get.........
}
我的控制器看起来像:
@RequestMapping(value="/uploadFileByPartner", method=RequestMethod.GET)
public @ResponseBody String uploadFileByPartner(WebRequest webrequest, HttpServletRequest request){
Map<String, String[]> parameters = webrequest.getParameterMap();
System.out.println("passed in parameters : "+parameters.toString());
return null;
}
当我尝试通过http发送它时,我无法使用POST,我必须使用GET。当我使用POST时,我的tomcat会激活http状态400。
当我在我的firefox firebug中检查我的http连接时,请求参数为:
[object Object]
没有其他详细信息。
卡塔利娜打印出来:
passed in parameters : {[object Object]=[Ljava.lang.String;@110732aa}
似乎是二进制字符串或字节数组。
我无法从HttpServletRequest中检索对象。
我试图使用:
@RequestBody FileEntity file
在请求参数中接受JSON对象注入,但我从来没有让它工作。
我被封锁了。
欢迎所有建议和评论。
答案 0 :(得分:0)
I found the solution by my self.
Have to make JSON map pair match with POJO entity fields. set
@RequestBody FileEntity file
as parameter to be inject into.
all works fine from now.