如何处理 Post 方法中的 MissingServletRequestParameterException 以在 Spring Boot 中丢失某些信息

How to handle MissingServletRequestParameterException in Post Method for missing some information in SpringBoot

在我的程序中,我有 class 个具有 4 个参数的玩家。 在 Post 方法中,如果我缺少参数,请尝试抛出 handleMissingParameter。 但是在 PostMan 中,当我像这样输入 json 时: { "nickname":"something", "fisrtname":"", "lastname":"something", "email":""

}

没有错误,一切顺利,显示我已创建!信息。 你能帮我看看我错在哪里吗?

////////////////////////////////////////// //// 这是我的 Post 方法:

@PostMapping
public ResponseEntity<?> createPlayer(@RequestBody Player player) throws MissingServletRequestParameterException {
    Player findplayer = repo.findByNickname(player.getNickname());
    if(findplayer != null) {
        return ResponseEntity.status(409).body("Conflict!");
    }       
    repo.save(player);
    return ResponseEntity.status(201).body("Created!");


}

//////////////////////////////////

这是我的 handleException,与我的 Post 方法位于同一位置:

@ExceptionHandler(MissingServletRequestParameterException.class)
public void handleMissingParams(MissingServletRequestParameterException ex) {
    String name = ex.getParameterName();
    System.out.println(name + " parameter is missing");
    }

////////////////////////////////////////// ///////

这是我的播放器 Class:

    package thesisMongoProject;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "player")
public class Player {
    @Id
    private String nickname;
    private String firstname;
    private String lastname;
    private String email;

    public Player(String nickname, String firstname, String lastname, String email) {
        super();
        this.nickname = nickname;
        this.firstname = firstname;
        this.lastname = lastname;
        this.email = email;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    @Override
    public String toString() {
        return "Player [nickname=" + nickname + ", firstname=" + firstname + ", lastname=" + lastname + ", email="
                + email + "]";
    }


}

//////////////////////////

不要使用 MissingServletRequestParameterException,而是使用 Bean Vaidation。

首先给Player参数加上@Valid注解:

@PostMapping
public ResponseEntity<?> createPlayer(@RequestBody @Valid Player player) {
    Player findplayer = repo.findByNickname(player.getNickname());
    if(findplayer != null) {
        return ResponseEntity.status(409).body("Conflict!");
    }       
    repo.save(player);
    return ResponseEntity.status(201).body("Created!");
}

然后向播放器添加验证:

@Document(collection = "player")
public class Player {
    @Id
    @NotBlank
    private String nickname;
    @NotBlank
    private String firstname;
    @NotBlank
    private String lastname;
    @NotBlank
    private String email;

查看这篇文章:https://www.baeldung.com/spring-boot-bean-validation