0
votes

I am trying to get user inputs from form on a Spring Boot application. What do I need to do?

I implement a login controller uses a @RequestParam annotation to request the data for validation but getting a result that parameter is not present.

// LoginController.java

@PostMapping(path = "/login")
public String userLogin(@RequestParam("email") String email, 
    @RequestParam("password") String password) {

    // validate username and password
    User user = userRepository.findUserByEmail(email);
    System.out.println(user.getEmail()+" "+user.getPassword());
    if(!(user.getEmail()==email) || !(user.getPassword()==password)){
        logger.info("Invalid user details or not register");
        return "login failed";
    }

    try{
        loginService.login(email, password);
        logger.info("User successfully login");
        return "redirect:dashboard/profile";
    }catch (Exception ex){
        logger.info("",ex);
    }
    return "Successfully logged in ..";
}

Login.html

<form th:action="@{/login}" method="post" class="form-horizontal" role="form">
    <form:hidden path/>
    <div class="form-group">
        <div class="mb-3">
            <input type="email" class="form-control" name="email" id="email" placeholder="[email protected]" required="required">
        </div>
    </div>

    <div class="form-group">
        <div class="mb-3">
            <input type="password" class="form-control" name="password" id="password" placeholder="" required="required">
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <label>
                <input type="checkbox"> Remember me
            </label>
            <button type="submit" class="btn btn-success">Login</button>
        </div>
    </div>
</form>

I expect the user email and password before he can login but get

2019-07-27 20:30:54.393 WARN 2900 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'email' is not present]

2
Include code on where you are calling the the APICoder
why are you submitting form values as param? you can simply submit as a form in requestBody and bind data in a POJO to use later.user404
Enable debug logging and check the URLs that get called and resolved. Also are you really submitting a form or are you also using a JavaScript framework to handle the actual form submit?M. Deinum

2 Answers

-1
votes

You must send the paramater name from front end :

   const payload: FormData = new FormData();
   payload.append("email", emai);
   payload.append("password", password);
-1
votes

The issue because of bad API design and RequestParam mismatch in your URL.

Change your API to this:

@PostMapping(path = "/login") public String userLogin(@RequestBody Map<String, Object> user) {
          String username=user.get("user").toString();
          String password=user.get("password").toString();
}

And your request body JSON should be like this.

{
"user":"USER",
"password":"pass5&234rf"
}

This is the JSON payload(body) for the POST request from the client side.