3
votes

Trying to create a basic web service uing jpa/hibernate. But beans are not being intialized. can anyone help me on this ?

below is my 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;
    }
}

below is my CustomerService.java:

@Service
public class CustomerService {

    @Autowired
    CustomerRepository repo;

    public List<CustomerModel> getCustomers() {
        List<CustomerModel> resp=repo.getAllCustomers();
        return resp;
    }


}

below is my CustomerRepository.java:

@Repository
public interface CustomerRepository extends CrudRepository<CustomerModel, Integer>{

    List<CustomerModel> getAllCustomers();
}

below is my 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: Error creating bean with name 'customerController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerService': Unsatisfied dependency expressed through field 'repo'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.ekart.fabfeet.service.CustomerRepository.getAllCustomers()! No property getAllCustomers found for type CustomerModel!

org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:116) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:397) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1429) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.0.RELEASE.jar:2.2.0.RELEASE]

3
tried that before. but still the same. following is the error log for that Failed to create query for method public abstract java.util.List com.ekart.fabfeet.service.CustomerRepository.getAllCustomerModels()! No property getAllCustomerModels found for type CustomerModel!Bhanu Prakash

3 Answers

1
votes

On CrudRepository you can use the findAll() method, doc here

When listing objects I recommend you extend from PagingAndSortingRepository, there you will have the implementations done for paging and sorting which is really handy.

Regarding the error, you can find the correct syntax here (getAll does not exist, you should use findAll).

0
votes

In your repository you are extending CrudRepository<CustomerModel, Integer>, but the ID column for your entity is string and not integer.

This is why it does not able to match the repository for you. Fix it to String and it should work properly

0
votes

apparently he is trying to implement a QueryMethods, in which case the method signature is incorrect, QueryMethods is a model in which the queries are built from the name of the properties of an entity and the keywords supported in the method names.