0

I am developing a rest API, but it provides null with @RequestBody. I went through the answers on this platform but it didn't solve my problem. I am unable to find where the problem is. I want to verify the user by validating userEmail and userPassword but didn't succeed.


@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService userService;

    @GetMapping("validateUser")
    public ResponseEntity<Boolean> validateUser(@RequestBody Users user) {
        boolean validateStatus = userService.validateUser(user.getUserEmail(), user.getUserPassword());
        if (validateStatus)
            return new ResponseEntity<Boolean>(validateStatus, HttpStatus.OK);
        else
            return new ResponseEntity<Boolean>(validateStatus, HttpStatus.UNAUTHORIZED);

    }

and pojo class as this:


    package com.ravionics.user.entity;    
    import java.io.Serializable;    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @Entity
    @Table(name="users")
    public class Users implements Serializable {
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name="user_Id")
        private int userId;  
        @Column(name="user_Name")
        private String userName;
        @Column(name="user_Email")  
        private String userEmail;
        @Column(name="user_Password")
        private String userPassword;

        public int getUserId() {
            return userId;
        }
        public void setUserId(int userId) {
            userId = userId;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            userName = userName;
        }
        public String getUserEmail() {
            return userEmail;
        }
        public void setUserEmail(String userEmail) {
            userEmail = userEmail;
        }
        public String getUserPassword() {
            return userPassword;
        }
        public void setUserPassword(String userPassword) {
            userPassword = userPassword;
        }       

    }

Reg
  • 10,717
  • 6
  • 37
  • 54
ravi
  • 1
  • 2
  • Make sure that your request body keys matches to that of the pojo properties. I think the issue lies in the mapping of the fields. for better clarity please post the request you are sending from the client. – raj240 May 12 '20 at 07:17
  • { "userEmail": "test@gmail.com", "userPassword": "test" } – ravi May 12 '20 at 07:20
  • First thing, change the request mapping to this: @PostMapping("/user") – Ugnes May 12 '20 at 07:22
  • 2
    The problem is your entity. You are assigning the values to itself. use `this.userEmail=userEmail` instead of `userEmail=userEmail` (or change the name of the method argument to something with a different name!). The first will assign the value to the `userEmail` field of the entity, the second will reassign the value to the method argument leaving the value `null`. – M. Deinum May 12 '20 at 07:29
  • As you suggested to make change to this.userEmail=userEmail, similarly i have also changes for userPassword as well and problem gets resolved. Thanks M Deinum – ravi May 12 '20 at 08:37

0 Answers0