Search code examples
spring-bootspring-mvcexceptionpostmanspring-restcontroller

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


In my program I have a class of players with 4 parameters. In the Post method, if I have missing parameter, try to throw handleMissingParameter. But in PostMan when i type as a json like this: { "nickname":"something", "fisrtname":"", "lastname":"something", "email":""

}

instead of having error, anything goes well and shows me created! message. could you help me where i made mistake, please!

///////////////////////////////////////////////// this is my Post Method:

@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!");


}

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

here is my handleException in the same place as my Post method is:

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

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

and here is my Player 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 + "]";
    }


}

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


Solution

  • Dont' use MissingServletRequestParameterException but use Bean Vaidation.

    First add @Valid annotation to the Player parameter:

    @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!");
    }
    

    Then add validation to the Player:

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

    Check out this article: https://www.baeldung.com/spring-boot-bean-validation