Error creating bean with name Spring Boot

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

Error creating bean with name Spring Boot

问题

以下是您要求的代码部分的翻译:

LoginController

  1. @PostMapping(path = "/login")
  2. public String saveEmployee(@RequestBody LoginDTO loginDTO)
  3. {
  4. String name = employeeService.loginEmployee(loginDTO);
  5. return "success";
  6. }

LoginDTO.java

  1. public class LoginDTO {
  2. private String email;
  3. private String password;
  4. public LoginDTO() {
  5. }
  6. public LoginDTO(String email, String password) {
  7. this.email = email;
  8. this.password = password;
  9. }
  10. public String getEmail() {
  11. return email;
  12. }
  13. public void setEmail(String email) {
  14. this.email = email;
  15. }
  16. public String getPassword() {
  17. return password;
  18. }
  19. public void setPassword(String password) {
  20. this.password = password;
  21. }
  22. @Override
  23. public String toString() {
  24. return "LoginDTO{" +
  25. "email='" + email + '\'' +
  26. ", password='" + password + '\'' +
  27. '}';
  28. }
  29. }

Employee.java

  1. @Entity
  2. @Table(name="employee")
  3. public class Employee {
  4. @Id
  5. @Column(name="employee_id", length = 45)
  6. @GeneratedValue(strategy = GenerationType.AUTO)
  7. private int employeeid;
  8. @Column(name="employee_name", length = 255)
  9. private String employeename;
  10. @Column(name="email", length = 255)
  11. private String email;
  12. @Column(name="password", length = 255)
  13. private String password;
  14. public Employee() {
  15. }
  16. public Employee(int employeeid, String employeename, String email, String password) {
  17. this.employeeid = employeeid;
  18. this.employeename = employeename;
  19. this.email = email;
  20. this.password = password;
  21. }
  22. public int getEmployeeid() {
  23. return employeeid;
  24. }
  25. public void setEmployeeid(int employeeid) {
  26. this.employeeid = employeeid;
  27. }
  28. public String getEmployeename() {
  29. return employeename;
  30. }
  31. public void setEmployeename(String employeename) {
  32. this.employeename = employeename;
  33. }
  34. public String getEmail() {
  35. return email;
  36. }
  37. public void setEmail(String email) {
  38. this email = email;
  39. }
  40. public String getPassword() {
  41. return password;
  42. }
  43. public void setPassword(String password) {
  44. this.password = password;
  45. }
  46. @Override
  47. public String toString() {
  48. return "Employee{" +
  49. "employeeid=" + employeeid +
  50. ", employeename='" + employeename + '\'' +
  51. ", email='" + email + '\'' +
  52. ", password='" + password + '\'' +
  53. '}';
  54. }
  55. }

Repo

  1. @EnableJpaRepositories
  2. @Repository
  3. public interface EmployeeRepo extends JpaRepository<Employee, Integer> {
  4. public Employee findByUsernamePassword(String username, String password);
  5. }

EmployeeService.java

  1. public interface EmployeeService {
  2. String addEmployee(EmployeeDTO employeeDTO);
  3. String loginEmployee(LoginDTO loginDTO);
  4. }

EmployeeIMPL.java

  1. public String loginEmployee(LoginDTO loginDTO) {
  2. Employee employee = employeeRepo.findByUsernamePassword(loginDTO.getEmail(), loginDTO.getPassword);
  3. if (employee == null) {
  4. return "Employee ID Not Found";
  5. } else {
  6. return "Logged In";
  7. }
  8. }
英文:

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

  1. {
  2. status = &quot;true&quot;&#39;
  3. message = &quot;login success&quot;
  4. }

Full Error

  1. 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;
  2. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-5.3.23.jar:5.3.23]
  3. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) ~[spring-beans-5.3.23.jar:5.3.23]
  4. at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.23.jar:5.3.23]
  5. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.23.jar:5.3.23]
  6. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) ~[spring-beans-5.3.23.jar:5.3.23]
  7. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.23.jar:5.3.23]
  8. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.23.jar:5.3.23]
  9. at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.23.jar:5.3.23]
  10. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.23.jar:5.3.23]
  11. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.23.jar:5.3.23]
  12. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.23.jar:5.3.23]
  13. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListabl

LoginController

  1. @PostMapping(path = &quot;/login&quot;)
  2. public String saveEmployee(@RequestBody LoginDTO loginDTO)
  3. {
  4. String name = employeeService.loginEmployee(loginDTO);
  5. return &quot;success&quot;;
  6. }

LoginDTO.java

  1. public class LoginDTO {
  2. private String email;
  3. private String password;
  4. public LoginDTO() {
  5. }
  6. public LoginDTO(String email, String password) {
  7. this.email = email;
  8. this.password = password;
  9. }
  10. public String getEmail() {
  11. return email;
  12. }
  13. public void setEmail(String email) {
  14. this.email = email;
  15. }
  16. public String getPassword() {
  17. return password;
  18. }
  19. public void setPassword(String password) {
  20. this.password = password;
  21. }
  22. @Override
  23. public String toString() {
  24. return &quot;LoginDTO{&quot; +
  25. &quot;email=&#39;&quot; + email + &#39;\&#39;&#39; +
  26. &quot;, password=&#39;&quot; + password + &#39;\&#39;&#39; +
  27. &#39;}&#39;;
  28. }
  29. }

Employee.java

  1. @Entity
  2. @Table(name=&quot;employee&quot;)
  3. public class Employee {
  4. @Id
  5. @Column(name=&quot;employee_id&quot;, length = 45)
  6. @GeneratedValue(strategy = GenerationType.AUTO)
  7. private int employeeid;
  8. @Column(name=&quot;employee_name&quot;, length = 255)
  9. private String employeename;
  10. @Column(name=&quot;email&quot;, length = 255)
  11. private String email;
  12. @Column(name=&quot;password&quot;, length = 255)
  13. private String password;
  14. public Employee() {
  15. }
  16. public Employee(int employeeid, String employeename, String email, String password) {
  17. this.employeeid = employeeid;
  18. this.employeename = employeename;
  19. this.email = email;
  20. this.password = password;
  21. }
  22. public int getEmployeeid() {
  23. return employeeid;
  24. }
  25. public void setEmployeeid(int employeeid) {
  26. this.employeeid = employeeid;
  27. }
  28. public String getEmployeename() {
  29. return employeename;
  30. }
  31. public void setEmployeename(String employeename) {
  32. this.employeename = employeename;
  33. }
  34. public String getEmail() {
  35. return email;
  36. }
  37. public void setEmail(String email) {
  38. this.email = email;
  39. }
  40. public String getPassword() {
  41. return password;
  42. }
  43. public void setPassword(String password) {
  44. this.password = password;
  45. }
  46. @Override
  47. public String toString() {
  48. return &quot;Employee{&quot; +
  49. &quot;employeeid=&quot; + employeeid +
  50. &quot;, employeename=&#39;&quot; + employeename + &#39;\&#39;&#39; +
  51. &quot;, email=&#39;&quot; + email + &#39;\&#39;&#39; +
  52. &quot;, password=&#39;&quot; + password + &#39;\&#39;&#39; +
  53. &#39;}&#39;;
  54. }
  55. }

Repo

  1. @EnableJpaRepositories
  2. @Repository
  3. public interface EmployeeRepo extends JpaRepository&lt;Employee,Integer&gt; {
  4. public Employee findByUsernamePassword(String username, String password);
  5. }

EmployeeService.java

  1. public interface EmployeeService {
  2. String addEmployee(EmployeeDTO employeeDTO);
  3. String loginEmployee(LoginDTO loginDTO);
  4. }

EmployeeIMPL.java

  1. public String loginEmployee(LoginDTO loginDTO) {
  2. Employee employee = employeeRepo.findByUsernamePassword(loginDTO.getEmail(),loginDTO.getPassword());
  3. if(employee == null)
  4. {
  5. return &quot;Employee ID Not Found&quot;;
  6. }
  7. else
  8. {
  9. return &quot;Logged In&quot;;
  10. }
  11. }

答案1

得分: 3

错误消息很明确。找不到类型为“Employee”的属性'usernamePassword'。

它期望在'Employee'内部有'usernamePassword'字段,但在您的'Employee.java'中您有电子邮件和密码?

您可能想要将findByUsernamePassword(String username, String password); 更改为:

  1. public Employee findOneByEmailAndPassword(String email, String password);

在'Employee.java'中创建一个String username字段,以及它的getter和setter。然后将存储库方法更改为

  1. 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:

  1. 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

  1. 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:

确定