【发布时间】:2020-02-23 05:56:37
【问题描述】:
尝试使用 jpa/hibernate 创建一个基本的 Web 服务。但是bean没有被初始化。谁能帮我解决这个问题?
下面是我的 CustomerController.java :
@RestController
public class CustomerController {
@Autowired
CustomerService service;
@SuppressWarnings("deprecation")
@PostMapping(value = "/getCust", consumes=MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<CustomerModel> retriveCustomers(@RequestBody CustomerModel cust){
System.out.println(cust); //just to see the object in console
List<CustomerModel> resp = service.getCustomers();
return resp;
}
}
下面是我的 CustomerService.java:
@Service
public class CustomerService {
@Autowired
CustomerRepository repo;
public List<CustomerModel> getCustomers() {
List<CustomerModel> resp=repo.getAllCustomers();
return resp;
}
}
下面是我的 CustomerRepository.java:
@Repository
public interface CustomerRepository extends CrudRepository<CustomerModel, Integer>{
List<CustomerModel> getAllCustomers();
}
下面是我的 CustomerModel.java:
@Entity
@Table(name="aliens")
public class CustomerModel {
@Id
@Column(name="customer_id")
private String customerId;
@Column(name="customer_name")
private String customerName;
@Column(name="customer_email")
private String customerEmail;
@Column(name="customer_phoneNum")
private String customerPhoneNum;
@Column(name="customer_password")
private String customerPassword;
}
org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“customerController”的 bean 时出错:不满意 通过字段“服务”表达的依赖关系;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“customerService”的 bean 时出错:不满意 通过字段“repo”表达的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为“customerRepository”的bean:调用init 方法失败;嵌套异常是 java.lang.IllegalArgumentException: 无法为方法 public abstract java.util.List 创建查询 com.ekart.fabfeet.service.CustomerRepository.getAllCustomers()!不 找到类型 CustomerModel 的属性 getAllCustomers!
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:397) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1429) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE] 在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]
【问题讨论】:
-
之前尝试过。但还是一样。以下是 Failed to create query for method public abstract java.util.List com.ekart.fabfeet.service.CustomerRepository.getAllCustomerModels() 的错误日志!找不到类型 CustomerModel 的属性 getAllCustomerModels!
标签: java hibernate spring-boot jpa