简体   繁体   中英

What is advantage of having interface in Spring Boot REST API / Micro services?

I have DAO interface and DAOImpl class on my Spring boot REST API project. But some of my teammates were telling me that it is not necessary to have an interface in REST API.

Their argument is if the code is shared as a .jar file (eg. Database Driver jar file) then it is necessary to have an interface. Since jar is added in the classpath and the end user can override the methods defined in the jar file. Whereas REST API's are deployed in servers and can be consumed by any programming language via HTTP

I know there is no concrete answer for this. But I just want to know the advantages/disadvantages of having an interface in REST API / Microservice

使用接口的好处是当您使用所有 Rest API 通用的服务时,例如带有 spring-security 的安全服务。

"interface" is NOT specific to REST API (or) Spring, it is one of the Object oriented principle, in Java programming terms, it is "interface".

Your friends are partially true.

In general "interface" are used to define the "protocol" and let the implementation be specific to "implementer".

Now the counter argument I can make is, what if your company wants to standardize the process between "departments" (or) "projects", specific to this DAO business specification (forgot about sharing as .jar or something else)?

接口是这种情况下的最佳实践,实现需要 1 分钟。

Once I did a hiring test to a company in UK and i got this feedback:

"And the dreaded Impl classes…"

At this point I started questioning the use of interfaces in microservices where everything is accessed through HTTP. After putting some thoughts into this I realized I never had a situation where Interfaces were really helpful in this situation.

I built a few personal projects without them (unless it's required by Spring) and I liked it. Being able to change services without painting them red by the IDE was a big plus.

Also, it's less code to maintain. I would vote up for it for new projects.

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