简体   繁体   中英

Should REST API calls be on repository layer with @Repository?

In my actual project we are in a conundrum, in the teams I've been in other companies we always did API calls to retrieve data on the Repository layer with @Repository from SpringBoot even if they were a simple RestTemplate getForEntity. But here I did as always did and we started debating about if it should be on the repository layer, called repository and if it needs the @Repository annotation at all and could just go with @Service or @Component.

Personally it makes sense that all data access layer operations should be on the repository layer even if its an API or a database as long as it retrieves data it should be there, the only thing I have doubts is about using @Repository since that is more for Spring Data own repositories.

What do you think should API calls be on the repository layer or on the service layer?

From the Javadoc of @Repository :

Indicates that an annotated class is a "Repository", originally defined by Domain-Driven Design (Evans, 2003) as "a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects". Teams implementing traditional Java EE patterns such as "Data Access Object" may also apply this stereotype to DAO classes, though care should be taken to understand the distinction between Data Access Object and DDD-style repositories before doing so. This annotation is a general-purpose stereotype and individual teams may narrow their semantics and use as appropriate.

A class thus annotated is eligible for Spring DataAccessException translation when used in conjunction with a PersistenceExceptionTranslationPostProcessor. The annotated class is also clarified as to its role in the overall application architecture for the purpose of tooling, aspects, etc.

As of Spring 2.5, this annotation also serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.

As you can see, it is not specifically for Spring Data Repositories.

While Spring Data detects @Repository , you can safely use them for other repositories, too, at least if it is meant for data access.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM