【问题标题】:Spring boot response not getting proper output春季启动响应没有得到正确的输出
【发布时间】:2020-04-24 15:01:46
【问题描述】:

我想创建一个简单的 Spring Boot 应用程序,它采用 JSON 的形式,并将输入与给定的凭据进行比较,即 username:1111 和 password:1 。但我无法将输入值与上述值进行比较。

 //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();
        }
    }
}

//登录对象类//

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

结果应该是:欢迎。但是在这里我没有得到预期的输出。

【问题讨论】:

标签: java spring-boot


【解决方案1】:

您必须使用equals 而不是==

if("1111".equals(login_credential.getUsername()) && "1".equals(login_credential.getPassword())) {
    return "welcome";
}

【讨论】:

    猜你喜欢
    • 2017-06-19
    • 2019-10-21
    • 2017-01-03
    • 2019-09-21
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多