英文:
Error creating bean with name Spring Boot
问题
以下是您要求的代码部分的翻译:
LoginController
@PostMapping(path = "/login")
public String saveEmployee(@RequestBody LoginDTO loginDTO)
{
String name = employeeService.loginEmployee(loginDTO);
return "success";
}
LoginDTO.java
public class LoginDTO {
private String email;
private String password;
public LoginDTO() {
}
public LoginDTO(String email, String password) {
this.email = email;
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "LoginDTO{" +
"email='" + email + '\'' +
", password='" + password + '\'' +
'}';
}
}
Employee.java
@Entity
@Table(name="employee")
public class Employee {
@Id
@Column(name="employee_id", length = 45)
@GeneratedValue(strategy = GenerationType.AUTO)
private int employeeid;
@Column(name="employee_name", length = 255)
private String employeename;
@Column(name="email", length = 255)
private String email;
@Column(name="password", length = 255)
private String password;
public Employee() {
}
public Employee(int employeeid, String employeename, String email, String password) {
this.employeeid = employeeid;
this.employeename = employeename;
this.email = email;
this.password = password;
}
public int getEmployeeid() {
return employeeid;
}
public void setEmployeeid(int employeeid) {
this.employeeid = employeeid;
}
public String getEmployeename() {
return employeename;
}
public void setEmployeename(String employeename) {
this.employeename = employeename;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Employee{" +
"employeeid=" + employeeid +
", employeename='" + employeename + '\'' +
", email='" + email + '\'' +
", password='" + password + '\'' +
'}';
}
}
Repo
@EnableJpaRepositories
@Repository
public interface EmployeeRepo extends JpaRepository<Employee, Integer> {
public Employee findByUsernamePassword(String username, String password);
}
EmployeeService.java
public interface EmployeeService {
String addEmployee(EmployeeDTO employeeDTO);
String loginEmployee(LoginDTO loginDTO);
}
EmployeeIMPL.java
public String loginEmployee(LoginDTO loginDTO) {
Employee employee = employeeRepo.findByUsernamePassword(loginDTO.getEmail(), loginDTO.getPassword);
if (employee == null) {
return "Employee ID Not Found";
} else {
return "Logged In";
}
}
英文:
I am new to using Spring Boot. I have already got the user registration form and API working fine and successfully added the records into the database. After completing the login API I ran into the problem with the attached error below. I have attached what I have tried below.
I want to display if the login was successful. Displayed as a JSON format it looks like this, I need help, please help me to correct the code below
{
status = "true"'
message = "login success"
}
Full Error
ework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeIMPL': Unsatisfied dependency expressed through field 'employeeRepo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepo' defined in com.example.Registation.Repo.EmployeeRepo defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract com.example.Registation.Entity.Employee com.example.Registation.Repo.EmployeeRepo.findByUsernamePassword(java.lang.String,java.lang.String); Reason: Failed to create query for method public abstract com.example.Registation.Entity.Employee com.example.Registation.Repo.EmployeeRepo.findByUsernamePassword(java.lang.String,java.lang.String)! No property 'usernamePassword' found for type 'Employee'; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.example.Registation.Entity.Employee com.example.Registation.Repo.EmployeeRepo.findByUsernamePassword(java.lang.String,java.lang.String)! No property 'usernamePassword' found for type 'Employee'
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-5.3.23.jar:5.3.23]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) ~[spring-beans-5.3.23.jar:5.3.23]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.23.jar:5.3.23]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.23.jar:5.3.23]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) ~[spring-beans-5.3.23.jar:5.3.23]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.23.jar:5.3.23]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.23.jar:5.3.23]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.23.jar:5.3.23]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.23.jar:5.3.23]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.23.jar:5.3.23]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.23.jar:5.3.23]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListabl
LoginController
@PostMapping(path = "/login")
public String saveEmployee(@RequestBody LoginDTO loginDTO)
{
String name = employeeService.loginEmployee(loginDTO);
return "success";
}
LoginDTO.java
public class LoginDTO {
private String email;
private String password;
public LoginDTO() {
}
public LoginDTO(String email, String password) {
this.email = email;
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "LoginDTO{" +
"email='" + email + '\'' +
", password='" + password + '\'' +
'}';
}
}
Employee.java
@Entity
@Table(name="employee")
public class Employee {
@Id
@Column(name="employee_id", length = 45)
@GeneratedValue(strategy = GenerationType.AUTO)
private int employeeid;
@Column(name="employee_name", length = 255)
private String employeename;
@Column(name="email", length = 255)
private String email;
@Column(name="password", length = 255)
private String password;
public Employee() {
}
public Employee(int employeeid, String employeename, String email, String password) {
this.employeeid = employeeid;
this.employeename = employeename;
this.email = email;
this.password = password;
}
public int getEmployeeid() {
return employeeid;
}
public void setEmployeeid(int employeeid) {
this.employeeid = employeeid;
}
public String getEmployeename() {
return employeename;
}
public void setEmployeename(String employeename) {
this.employeename = employeename;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Employee{" +
"employeeid=" + employeeid +
", employeename='" + employeename + '\'' +
", email='" + email + '\'' +
", password='" + password + '\'' +
'}';
}
}
Repo
@EnableJpaRepositories
@Repository
public interface EmployeeRepo extends JpaRepository<Employee,Integer> {
public Employee findByUsernamePassword(String username, String password);
}
EmployeeService.java
public interface EmployeeService {
String addEmployee(EmployeeDTO employeeDTO);
String loginEmployee(LoginDTO loginDTO);
}
EmployeeIMPL.java
public String loginEmployee(LoginDTO loginDTO) {
Employee employee = employeeRepo.findByUsernamePassword(loginDTO.getEmail(),loginDTO.getPassword());
if(employee == null)
{
return "Employee ID Not Found";
}
else
{
return "Logged In";
}
}
答案1
得分: 3
错误消息很明确。找不到类型为“Employee”的属性'usernamePassword'。
它期望在'Employee'内部有'usernamePassword'字段,但在您的'Employee.java'中您有电子邮件和密码?
您可能想要将findByUsernamePassword(String username, String password);
更改为:
public Employee findOneByEmailAndPassword(String email, String password);
或
在'Employee.java'中创建一个String username
字段,以及它的getter和setter。然后将存储库方法更改为
public Employee findOneByUsernameAndPassword(String username, String password);
阅读此以获取更多详细信息:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation
英文:
The error message is pretty clear. No property 'usernamePassword' found for type 'Employee'
It expect usernamePassword
field inside Employee
, but in your Employee.java
you have email and password?
You might want to change findByUsernamePassword(String username, String password);
to:
public Employee findOneByEmailAndPassword(String email, String password);
Or
Create a String username
field in Employee.java
with its getters and setters. Then change the repository method to
public Employee findOneByUsernameAndPassword(String username, String password);
Read this for more details https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论