javascript - JQuery/Ajax & Spring Rest Multi-part form submit -
i quite new jquery , trying asynchronous multipart form uploading. form consist of few data fields , file type. have set server side code (spring) this:
@requestmapping(method = requestmethod.post) public @responsebody upload multiplesave(multiparthttpservletrequest request) { upload upload = new upload(); iterator<string> iterator = request.getfilenames(); while (iterator.hasnext()) { multipartfile file = request.getfile(iterator.next()); try { system.out.println(messageformat.format("file length: {0}", arrays.tostring(file.getbytes()))); system.out.println("file type: " + file.getcontenttype()); upload.setcontent(file.getbytes()); upload.setdocid(id++); upload.seterror(null); upload.setname(file.getname()); upload.setsize(file.getsize()); filelist.put(upload.getdocid().tostring(), upload); } catch (exception e) { system.out.println("error occurred: " + e); upload.seterror("500: went wrong!"); } } return upload; }
and client side code this:
function processfileupload() { console.log("fileupload clicked"); var formdata = new formdata(); formdata.append("file", files[0]); $.ajax({datatype: 'json', url: "/springjqueryfileupload/upload", data: formdata, type: "post", enctype: 'multipart/form-data', processdata: false, contenttype: false, success: function (result) { alert('success' + json.stringify(result)); }, error: function (result) { alert('error' + json.stringify(result)); } }); }
when submit, server responds this:
java.lang.illegalargumentexception: no converter found return value of type: class com.upload.model.upload
i wondering error. missing here??
i try changing annotation to:
@requestmapping(method = requestmethod.post, produces=mediatype.application_json_value)
and make sure have jackson (which spring uses json serialization) on path. also, make sure upload
class serializable, e.g. not private
or that. if normal java bean type class should fine.
lastly, if doesn't work can turn on spring debug logs like:
log4j.category.org.springframework.web=all
in log4j.properties file.
Comments
Post a Comment