必需的请求正文缺少从 Vue 到 Spring 的异常

Required request body is missing exception from Vue to Spring

我正在尝试将数据从 Vue 发送到 Spring

我到处搜索,一切看起来都很完美,但还是不行

起初我在想可能是因为我在后面的 class 的字段比我从前面发送的字段多,但即使我创建了另一个 class 具有相同的字段它不起作用

这是我的发送请求:

sendRequest() {
  console.log(this.user);
  (this.loading = true),
    axios.get("http://localhost:8081/user/log-in", this.user)
      .then((response) => {
        console.log(response);
      })
      .catch((error) => {
        (this.isShow = true),
          (this.isHide = false),
          (this.showAlert = true),
          (this.message = error.response.data);
        (this.showMessage = true),
          setTimeout(
            function () {
              this.showMessage = false;
              (this.isShow = false), (this.isHide = true);
            }.bind(this),
            3000
          );
      })
      .finally(() => {
        this.loading = false;
      });

这是我从前到后发送的数据:

      user: {
    userName: "",
    password: ""
  },

这是我获取数据的后端class:

public class UserSignUpDTO {

private String firstName;

private String userName;

private String email;

private String password;

private String birthDay;

}

这里是获取数据的方法:

    @GetMapping("/log-in")
public UserDTO logInUser(@RequestBody UserSignUpDTO user) throws Exception {
    return userService.logInUser(user);
}

请求映射:

@RestController
@RequestMapping("/user")
public class UserController {

It is not advisable to use a GET with a request body. While HTTP/1.1好像没有明确禁止这个,应该没有意义。

如提示here,

If you give it meaning by parsing it on the server and changing your response based on its contents, then you are ignoring this recommendation in the HTTP/1.1 spec, section 4.3.

我建议您在这种情况下使用 POST 请求。

改变这个

axios.get("http://localhost:8081/user/log-in", this.user)

到此

axios.post("http://localhost:8081/user/log-in", this.user)