缺少所需的请求正文 Spring 启动 Angular

Required request body is missing Spring boot Angular

我想 post 数据到我的 Spring 启动 API 网络服务这是我在 Rest Controller 中的功能:

@PostMapping(value = "/update_version")
public String updateVersion(@RequestBody String type, @RequestBody String buildId,
        @RequestBody String imageName, @RequestBody String remoteDirectory,
        @RequestBody String backupPath) {

    return historyService.updateVersion(type, buildId, imageName, remoteDirectory, backupPath);
}

我试过 post 和 angular 前面 project.I 认为它可能有问题所以我试过 post 人: 我该如何解决这个问题?

请求必须只有一个RequestBody.

您的方法参数中不能使用多个@RequestBody,这是Spring引导无法做到的,您只能使用一次。

解决方法:

1- 创建一个模型class 命名为例如History(根据您的要求)

2- 像这样指定所有需要的属性

public class History {

private String type;

@JsonProperty("build_id")
private String buildId;

@JsonProperty("image_name")
private String imageName;

@JsonProperty("remote_directory")
private String remoteDirectory;

@JsonProperty("backup_path")
private String backupPath;

//Getters + Setters + Constructor(s) etc ...

}

3- 方法签名会像这样

@PostMapping(value = "/update_version")
public String updateVersion(@RequestBody History history) { ... }