简体   繁体   English

@RepositoryRestController和@Repository注释之间有什么区别?

[英]What is the difference between @RepositoryRestController and the @Repository annotations?

I want to know what exactly the difference between using the annotation @RepositoryRestController and @Repository because I've tried them both and I find absolutely no difference! 我想知道使用注释@RepositoryRestController@Repository之间究竟有什么区别,因为我已经尝试了它们两个并且我发现完全没有区别!

I tried the following: 我尝试了以下方法:

@RepositoryRestResource
public interface MovieRepository extends JpaRepository<Movie, Short> { 
} 

and

@Repository
public interface MovieRepository extends JpaRepository<Movie, Short> { 
}

so when I try : /movies in both methods I get the same results. 所以当我尝试: /两种方法中的电影时 ,我得到相同的结果。

And if I use @RepositoryRestController should I use @RepositoryRestController , or I can use @RestController , and is there any difference between them? 如果我用@RepositoryRestController我应该使用@RepositoryRestController ,或者我可以使用@RestController ,并且它们之间有什么区别吗?

@Repository

@Repository is a stereotype interface for defining 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" . @Repository是一个@Repository型接口,用于将最初由Domain-Driven Design(Evans,2003)定义的存储库定义为“封装模拟对象集合的存储,检索和搜索行为的机制”

This annotation also serves as a specialization of @Component , allowing for implementation classes to be autodetected through classpath scanning. 此注释还用作@Component ,允许通过类路径扫描自动检测实现类。

@RepositoryRestResource

@RepositoryRestResource tells Spring Data REST to expose your repository as REST endpoints. @RepositoryRestResource告诉Spring Data REST将您的存储库公开为REST端点。 Check the relevant part of the documentation. 检查文档的相关部分。


If you want to write a custom handler for a specific resource taking advantage of Spring Data REST's settings, message converters, exception handling and more, you can use @RepositoryRestController (instead of the standard Spring MVC @Controller or @RestController annotations). 如果要利用Spring Data REST的设置,消息转换器,异常处理等为特定资源编写自定义处理程序,可以使用@RepositoryRestController (而不是标准的Spring MVC @Controller@RestController注释)。 See the relevant part of the documentation. 请参阅文档的相关部分。

The reason why it works in your case with or without the annotations, is because if you use Spring Boot together with Spring Data REST, in that case an auto configuration is activated, which automatically exposes all Spring Data interfaces as REST resource. 它在你的情况下有或没有注释的原因是因为如果你将Spring Boot与Spring Data REST一起使用,那么就会激活一个自动配置,它会自动将所有Spring Data接口公开为REST资源。 You can find the source code for this configuration here , and more about the possibilities to customize it in the reference documentation . 您可以在此处找到此配置的源代码,以及有关在参考文档中自定义它的可能性的更多信息。

@Repository:

Spring @Repository annotation denotes that the class provide mechanism for crud operations. Spring @Repository注释表示该类为crud操作提供机制。

You can find more information here . 您可以在此处找到更多信息。

@RepositoryRestResource:

This annotation gives RestController functionality to the repository. 此批注为存储库提供了RestController功能。 This means you can access your Repository directly. 这意味着您可以直接访问您的存储库。 You can read further about this here . 你可以在这里进一步阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @Entity 和 @Repository 注释有什么区别? - What is the difference between @Entity and @Repository annotations? 如何处理@Component和@Repository / @Service注释之间的区别是什么? - What's the difference between how @Component and @Repository / @Service annotations are processed? Spring 中的@Component、@Repository 和@Service 注解有什么区别? - What's the difference between @Component, @Repository & @Service annotations in Spring? Seedstack 中的 Repository 和 Finder 有什么区别? - What's the difference between a Repository and a Finder in Seedstack? "Spring 中的@Repository 和@RepositoryDe​​finition 有什么区别?" - What's the difference between @Repository and @RepositoryDefinition in Spring? JPA Repository 中的 DateGreaterThanEqual 和 DateIsGreaterThanEqual 有什么区别? - What is the difference between DateGreaterThanEqual and DateIsGreaterThanEqual in JPA Repository? Spring注释之间的区别 - Difference between Spring annotations @CascadeOnDelete和CascadeType.REMOVE批注有什么区别? - What's the difference between @CascadeOnDelete and CascadeType.REMOVE annotations? 字段之间有什么区别:注释数组与单独的注释 - What is the difference between field: array of annotation vs separate annotations Play控制器的@After和@Finally注释之间有什么区别? - What's the difference between @After and @Finally annotations for Play controllers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM