Reputation: 67
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.
Upvotes: 0
Views: 3728
Reputation: 13817
Reason: You are sending the login details as the JSON body which can be mapped using the @RequestBody
and you are using the @RequestParam
in which you have to pass the details as a query param. The ideal way is to create the DTO and use the the @RequestBody
When you use the @RequestParam
you have to send the details as the query parameters
URL: host:port/endpoint?param1=value1¶m2=value2
URL: http://localhost:8080/[email protected]&password=sbj123
@PostMapping("/login1")
@ResponseBody
public Response1 login(@RequestParam(name="email",required=false) String email, @RequestParam(name="password",required=false) String password) {
...
}
Currently, you are sending the details as the JSON body which can be mapped using the @RequestBody
and you have to create the DTO to map the keys.
class LoginRequestDTO {
public String email;
public String password;
}
@PostMapping("/login1")
@ResponseBody
public Response1 login(@RequestBody LoginRequestDTO loginRequest) {
...
}
//JSON body as input
{
"email": "[email protected]",
"password": "sbj123"
}
Here you may know more details on the spring boot annotations
Upvotes: 1
Reputation: 2422
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());
}
Upvotes: 1
Reputation: 2357
using json request body you'll need a pojo
class LoginRequest {
public String email;
public String password;
}
and change controller to
public Response1 login(@RequestBody LoginRequest loginRequest) {
Or send the login data as form params.
https://www.baeldung.com/spring-mvc-send-json-parameters
Upvotes: 1