user10244386
user10244386

Reputation:

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!

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?

Upvotes: 8

Views: 6129

Answers (3)

Farrukh Ahmed
Farrukh Ahmed

Reputation: 493

@Repository:

Spring @Repository annotation denotes that the class provide mechanism for crud operations.

You can find more information here.

@RepositoryRestResource:

This annotation gives RestController functionality to the repository. This means you can access your Repository directly. You can read further about this here.

Upvotes: 3

dunni
dunni

Reputation: 44515

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. You can find the source code for this configuration here, and more about the possibilities to customize it in the reference documentation.

Upvotes: 4

cassiomolin
cassiomolin

Reputation: 130867

@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".

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

@RepositoryRestResource

@RepositoryRestResource tells Spring Data REST to expose your repository as REST endpoints. 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). See the relevant part of the documentation.

Upvotes: 7

Related Questions