0

Angularjs $http.post gives a 404 exception but all the other methods works fine with the same codes, i am trying to upload a file via spring boot

The same codes works fine in my other project i did about last year, and the same $http.post works when i send information without a file

service.js

function addCompanyDoc(file, id) {
  var deferred = $q.defer();
  var data = new FormData();
  data.append('file', file);
  var config = {
    transformRequest: angular.identity,
    transformResponse: angular.identity,
    headers: {
      'Content-Type': undefined
    }
  }

  console.log('appService file ');
  console.dir(file); //cheking if file is available


  $http.post('http://localhost:8080/listed/welcome/company/doc/' + id,
      data, config)
    .then(function(response) {
        deferred.resolve(response.data);
      },
      function(errResponse) {
        alert(errResponse.data.errorMessage);
        console.error('Error while uploading company doc', errResponse);
        this.flag = 'failed';
        deferred.reject(errResponse);
      });

  return deferred.promise;
}

spring boot

@RequestMapping("/welcome")
public class controllers{

@RequestMapping(value = "/company/doc/{id}", method = 
RequestMethod.POST, consumes = {"multipart/form-data" })
@ResponseBody 
public ResponseEntity<Void> saveCompanyDoc(@RequestParam("file") 
MultipartFile file, @PathVariable final int id){
//....uploading to DB
} 
}

angularjs sends a document to spring boot and spring uploads to the DB / sends to a folder. Working fine :-) with angularjs 1.7 & spring boot 2.*

  • You never append anything to the FormData object. Note you don't need `$q.defer()` since `$http` methods already return a `$q` promise – charlietfl Jul 28 '19 at 17:40
  • Avoid the [deferred anti-pattern](https://stackoverflow.com/questions/30750207/is-this-a-deferred-antipattern). – georgeawg Jul 28 '19 at 17:52
  • Thanks @charlietfl i guess the append got deleted when i was copying the codes to stackoverflow but its there and also removed the $q and the same outcome occurs :-( – Memo 313 MediaSA Jul 28 '19 at 18:17
  • Now the company controller.js handles function success & error() bur still no luck :: var req = { method: 'POST', url: REST_SERVICE_URI+'company/doc/'+id, config, data: formData } return $http(req); and in the backend i removed ResponseBody and made the whole class RestController, and i tried to use RequestPart in space of RequestParam :: still no luck – Memo 313 MediaSA Jul 30 '19 at 11:42

1 Answers1

0

Thanks for all the comments the above code is correct,

I created a dummy table and controller to test the app.

And it gave the same 404 error and i recalled a few weeks back i added an "admin folder" to handle CMS and directed all the ROLES to the "folder" and it blocked all the users from doing data POSTING. SO i just had to fix my WebSecurityConfig class :-)