简体   繁体   中英

How to return of the params to the backend (springBoot)?

I am sending 2 params from the front end in the service below:

uploadFile(fileName: string, fileAsBase64:string): Observable<any> {
    let params = new HttpParams();
    params = params.append('fileName', fileName);
    params=params.append('fileAsBase64 ',fileAsBase64 )

    return  this.http.post('/api/uploadFile', params, { responseType: 'text' });
  }

But I receive only one paramaters: fileName=test fileAsBase64=null

@PostMapping(value ="/api/uploadFile")
public String uploadFile(@RequestBody String name , String fileAsBase64) {
   
}

You could wrap your two parameter in a dto

@PostMapping(value = "/api/v1/upload-file")
public String uploadFile(@RequestBody EncodedFile file)
{

}

record EncodedFile(String fileName, String fileAsBase64) {}

And you better version your api.

You need to use the @RequestParam annotation.

https://www.baeldung.com/spring-request-param

you function have to be as the following:

@PostMapping(value ="/api/uploadFile")
public String uploadFile(@RequestParam String fileName, @RequestParam String fileAsBase64) {   
}

OR you can put any name but you have to put the matched name within the annotation:

@PostMapping(value ="/api/uploadFile")
public String uploadFile(@RequestParam(name = "fileName") String name, @RequestParam(name = "fileAsBase64") String file) {   
}

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