0

Am new to Spring boot, i have seen example where we create repository to perform various operarion with the given Object. here is the sample one

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {

    List<Person> findByLastName(@Param("name") String name);

}

so from the rest client if i send People jason

http://localhost:8080/people{ .... }

it inserts to the Database, internally it calls save method.

Here after calling from the REST client , i want to do some validation or business login and then insert to the database, how can i do it? It means i want to call a service method to do all the business logic and then insert so how can i call service method from repository class?

Shri
  • 469
  • 5
  • 18
  • Can i know the reason for the down vote? – Shri Apr 04 '15 at 19:30
  • This question is not clear. Why do you want to call a service method from the repository class. "Repository" classes are meant to do the DB related work and having a service method is not related to "Spring-Boot" in anyway. Create the service class to perform the business logic then make a call to repository to perform the DB related operation. – Ripu Daman Apr 04 '15 at 19:31
  • So i cannot write any business logic in this pattern, i have to call service layer and then from there i need to call repository.. is this right? – Shri Apr 04 '15 at 19:34
  • Yes. You need to add a service class to perform the business logic and in that class, have your repository reference injected which you can use to make the calls to repository methods. – Ripu Daman Apr 04 '15 at 19:36
  • So in this case there is no meaning in adding "@RepositoryRestResource" annotation to Repository class, because we are not calling respository class from REST we have to call service and then service uses autowired object to call repository. am i right? – Shri Apr 04 '15 at 19:39
  • See this link http://stackoverflow.com/questions/22824840/when-to-use-restcontroller-vs-repositoryrestresource – Ripu Daman Apr 04 '15 at 19:42
  • Okay, here is my main question comes, with the same example in the link you sent me, i want to have a business logic upon Person object that comes in, how do i do it ? – Shri Apr 04 '15 at 19:47

1 Answers1

2

This repository is an interface and will allow you to perform various operations (here operations means DB related operations) involving Person objects. It gets these operations by extending the PagingAndSortingRepositry interface defined in Spring Data Commons.

At runtime, Spring Data REST will create an implementation of this interface automatically. Then it will use the @RepositoryRestResource annotation to direct Spring MVC to create RESTful endpoints at /people.

I don't think your requirements can be fulfilled with having the "@RepositoryRestResource" on the repository. You may want to create a proper sprint-boot application with the api, service and repo layer to perform the tasks you want to perform.

Ripu Daman
  • 2,482
  • 2
  • 18
  • 22