1

I want to create a simple spring boot application, where it takes in form of JSON and compares the input with given credential, i.e, username:1111 and password:1 . But I am not able to compare the input value to the above mentioned values.

 //Controller class//

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.models.Login_object;

@RestController
public class AppController {
    @PostMapping(path="/login")
    String login(@RequestBody Login_object login_credential) {
        if(login_credential.getUsername()=="1111" && login_credential.getPassword()=="1") {
            return "welcome";
        }
        else {          
            return "you entered:"+login_credential.getUsername()+":"+login_credential.getPassword();
        }
    }
}

//Login_object class//

public class Login_object {
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}

enter image description here

The result should be: Welcome. But here I am not getting the expected output.

Ak Agrl
  • 13
  • 3
  • Use equals, https://stackoverflow.com/questions/7520432/what-is-the-difference-between-and-equals-in-java, and this is not relevant to be asked in StackOverflow. – VostanAzatyan Apr 24 '20 at 15:09
  • Does this answer your question? [What is the difference between == and equals() in Java?](https://stackoverflow.com/questions/7520432/what-is-the-difference-between-and-equals-in-java) – Vikas Apr 24 '20 at 15:11
  • This is issue with understanding "==" & equals . https://www.java67.com/2012/11/difference-between-operator-and-equals-method-in.html This might help to understand – Sajith Vijesekara Apr 24 '20 at 19:41

1 Answers1

0

You have to use equals instead of ==,

if("1111".equals(login_credential.getUsername()) && "1".equals(login_credential.getPassword())) {
    return "welcome";
}
Vikas
  • 6,868
  • 4
  • 27
  • 41