简体   繁体   中英

angular 8 post rest service not working with spring boot - giving null in HttpServletRequest

I am working on a angular application. In that I have to send data to backend post api call . I am using 'formdata' to send file. But when I am checking java side in spring boot controller HttpServletRequest is don't have the data we send from angular (when I read userId from request I got null)

this below is my angular code

const formData = new FormData();
for(let j=0;j<this.fileLists.length; j++)
{
  formData.append("files",<File>this.fileLists[j]);
}
formData.append('userId','magic123');
this.homeService.uploadFiles(formData).subscribe(data => {
  console.log("result is ", data)
})


uploadFiles(formdata){
return this.http.post("upload url",formdata);
}

and the below is my java code

java

@CrossOrigin(origins = ("http://localhost:4200"))
@PostMapping(value = "/upload" )
public ResponseEntity<CdccUploadResponseVO> uploadJSON(HttpServletRequest httpRequest, 
HttpServletResponse httpResponse)
throws CALValidationException
{
  System.out.println("Controller================================get is"+httpRequest.getParameter("userId"));

I am getting null in httpRequest.getParameter("userId")

edit 1:when I tried in postman , API is working only when we are sending data as multipart form. otherwise the same the error like angular. So how can I send send Multipart form from angular instead of Formdata.

Please help me to fix this.

I think you have a typo in the Angular code, it should be with lowercase f :

return this.http.post("upload url", formData);

Also, "upload url" should match the url of Spring Boot @PostMapping but presumably you have just edited that out. I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM