Search code examples
springspring-bootpostpostman

@RequestParam return null value from Postman


I am trying to make a login Restful API using Spring Boot. I am using Postman to test API. But When I am passing email and password through postman it returns null parameters. Because of that, my other functionalities are not working. Here is my code:

LoginController

@PostMapping("/login1")
@ResponseBody
public Response1 login(@RequestParam(name="email",required=false) String email, @RequestParam(name="password",required=false) String password) {    
        
       System.out.println("Email is:"+email);
       System.out.println("Password is:"+password);
       return lgservice.checkLogin(email, password);      
}

PostMapping URL: http://localhost:8080/login1 I am sending the following data through postman:

{
    "email": "[email protected]",
    "password": "sbj123"
    
}

My Expected Output is this:

{
    "code": 200,
    "status": "Success",
    "message": "Login Successfull!",
    "college": [
     {
       "clgId": 50,
       "name": "SB Jain",
       "email": "[email protected]",
       "city": "nagpur"
     }
    ]
}

But I am getting this:

{
    "code": 500,
    "status": "Failed",
    "message": "Please enter valid email and password",
    "isSuccess": false
}

Logs

2021-05-07 17:18:48.750  INFO 11448 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-05-07 17:18:48.757  INFO 11448 --- [           main] s.c.CunsultustodayWebServicesApplication : Started CunsultustodayWebServicesApplication in 4.246 seconds (JVM running for 5.143)
2021-05-07 17:18:56.665  INFO 11448 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-05-07 17:18:56.665  INFO 11448 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-05-07 17:18:56.666  INFO 11448 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
Email is:null
Password is:null

If I am doing anything wrong please guide me.


Solution

  • This happens because @RequestParam stays for query parameters (@RequestParam JavaDoc). So, the correct usage of this API will be POST http://localhost:8080/[email protected]&password=pass.

    If you want to pass your parameters in request body, you need to use @RequestBody (JavaDoc) and create a POJO containing your email and password fields or use a Map (which I don't recommend doing). Here is an example

    // User.java
    public class User {
        
        private String email;
        private String password;
    
        // getters, setters, constructors, etc.
    }
    
    @PostMapping("/login1")
    @ResponseBody
    public Response1 login(@RequestBody User user) {    
           System.out.println("Email is: " + user.getEmail());
           System.out.println("Password is: " + user.getPassword());
           return lgservice.checkLogin(user.getEmail(), user.getPassword());      
    }