angularjs - How to send JSON from clinet and get DTO-Object on server? -
at client have:
for (var = 0; < $files.length; i++) { var file = $files[i]; var info = {f_name: 'test1', s_name: 'test2'}; var fd = new formdata(); fd.append('data', angular.tojson(data)); fd.append("file", file); $http({ method: 'post', url: 'employeeservice/employee/upload', headers: {'content-type': undefined}, data: fd, transformrequest: angular.identity }) .success(function(data, status) { alert("success"); }); }
and on server (spring):
@responsebody @requestmapping(value = "/upload", method = requestmethod.post) public string postfile(@requestparam(value="file", required=false) multipartfile file, @requestparam(value="data") object data) throws exception { system.out.println("data = " + data); homecoming "ok!"; }
but data
string: "{"f_name":"test1","s_name":"test2"}". have dto-class:
public class employeedto(){ private string f_name; private string s_name; //setters , getters }
and on server want get:
@responsebody @requestmapping(value = "/upload", method = requestmethod.post) public string postfile(@requestparam(value="file", required=false) multipartfile file, @requestparam(value="data") employeedto employeedto) throws exception { //etc. }
how send info client (file , data) , on server file , employeedto object?
while can utilize annotation @requestbody
object json string.
for example:
@responsebody @requestmapping(value = "/test", method = requestmethod.post) public string test(@requestbody employeedto employeedto){ //todo something; homecoming "success"; }
or can receive string , utilize objectmapper convert string object
for example:
employeedto employeedto = new objectmapper().readvalue("here json string", employeedto.class);
angularjs spring
No comments:
Post a Comment