Error creating bean with name Spring Boot

huangapple go评论54阅读模式
英文:

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 = &quot;true&quot;&#39;
message = &quot;login success&quot;
}

Full Error

ework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name &#39;employeeController&#39;: Unsatisfied dependency expressed through field &#39;employeeService&#39;; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name &#39;employeeIMPL&#39;: Unsatisfied dependency expressed through field &#39;employeeRepo&#39;; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;employeeRepo&#39; 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 &#39;usernamePassword&#39; found for type &#39;Employee&#39;; 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 &#39;usernamePassword&#39; found for type &#39;Employee&#39;
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 = &quot;/login&quot;)
public String saveEmployee(@RequestBody LoginDTO loginDTO)
{
String name = employeeService.loginEmployee(loginDTO);
return &quot;success&quot;;
}

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 &quot;LoginDTO{&quot; +
&quot;email=&#39;&quot; + email + &#39;\&#39;&#39; +
&quot;, password=&#39;&quot; + password + &#39;\&#39;&#39; +
&#39;}&#39;;
}
}

Employee.java

@Entity
@Table(name=&quot;employee&quot;)
public class Employee {
@Id
@Column(name=&quot;employee_id&quot;, length = 45)
@GeneratedValue(strategy = GenerationType.AUTO)
private int employeeid;
@Column(name=&quot;employee_name&quot;, length = 255)
private String employeename;
@Column(name=&quot;email&quot;, length = 255)
private String email;
@Column(name=&quot;password&quot;, 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 &quot;Employee{&quot; +
&quot;employeeid=&quot; + employeeid +
&quot;, employeename=&#39;&quot; + employeename + &#39;\&#39;&#39; +
&quot;, email=&#39;&quot; + email + &#39;\&#39;&#39; +
&quot;, password=&#39;&quot; + password + &#39;\&#39;&#39; +
&#39;}&#39;;
}
}

Repo

@EnableJpaRepositories
@Repository
public interface EmployeeRepo extends JpaRepository&lt;Employee,Integer&gt; {
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 &quot;Employee ID Not Found&quot;;
}
else
{
return &quot;Logged In&quot;;
}
}

答案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 &#39;usernamePassword&#39; found for type &#39;Employee&#39;

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

huangapple
  • 本文由 发表于 2023年2月8日 23:36:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定