简体   繁体   English

如何将参数返回到后端(springBoot)?

[英]How to return of the params to the backend (springBoot)?

I am sending 2 params from the front end in the service below:我在下面的服务中从前端发送 2 个参数:

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但我只收到一个参数: 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您可以将两个参数包装在 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.你最好版本你的 api。

You need to use the @RequestParam annotation.您需要使用 @RequestParam 注释。

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

you function have to be as the following:您 function 必须如下所示:

@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) {   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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