angularjs - Angular: how to post a file to the server on form submit -


i have form that's being posted back-end (kotlin, spring web). form had text inputs , post worked flawlessly. when added file input, post stopped working, returning following error:

{status: 400, error: "bad request",…} error: "bad request" exception: "org.springframework.http.converter.httpmessagenotreadableexception" message: "could not read document: no suitable constructor found type [simple type, class com.test.insertconfigcommand]: can not instantiate json object (missing default constructor or creator, or perhaps need add/enable type information?)↵ @ [source: java.io.pushbackinputstream@2cb43211; line: 1, column: 2]; nested exception com.fasterxml.jackson.databind.jsonmappingexception: no suitable constructor found type [simple type, class com.test.insertconfigcommand]: can not instantiate json object (missing default constructor or creator, or perhaps need add/enable type information?)↵ @ [source: java.io.pushbackinputstream@2cb43211; line: 1, column: 2]" 

here codes of stack:

view:

<form ng-submit="insert(config)">     <input type="text" ng-model="config.name">     <input type="text" ng-model="config.address">     <input type="file" ng-model="config.file">     <button type="submit">save</button> </form> 

controller (front-end):

$scope.insert = function (config) {     $http.post('/config', config)         .then(function (response) {             $.snackbar({content: "success!"});         }, $scope.showerrormessage); }; 

controller (back-end):

@requestmapping(method = arrayof(requestmethod.post)) fun insert(@requestbody config: insertconfigcommand) = service.save(config) 

insertconfigcommand

data class insertconfigcommand (     val name : string = "",     val address : string = "",     val file : multipartfile ) 

i've tried post following way, works, sends file:

controller (front-end):

$scope.insert = function (file) {     var fd = new formdata();     fd.append('file', file);      return $http.post('/config', fd, {         transformrequest: angular.identity,         headers: {             'content-type': undefined         }     }); }; 

controller (back-end):

@requestmapping(method = arrayof(requestmethod.post)) fun insert(@requestparam(value = "file", required = true) file: multipartfile) = service.save(file) 

what need change post work? want send input file on same object name , address.

i suppose using jackson, right?

bytecode of data classes in kotlin looks differently 1 of regular pojo (with default constructor) , jackson unable initialize such class. try add dependency jackson kotlin module , make sure register it.

if use spring boot add following code of @configuration annotated classes:

@bean open fun kotlinmodule(): kotlinmodule {     return kotlinmodule() } 

and see if helps.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo