Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between @RestController and @RepositoryRestController

Tags:

What is the typical use case code that shows the difference between those two annotations - meaning the @RestController and the @RepositoryRestController - ?

like image 975
mCs Avatar asked Dec 29 '15 14:12

mCs


People also ask

What is difference between @RestController and @controller and explain?

@Controller annotation indicates that the class is a “controller” like a web controller. @RestController annotation indicates that class is a controller where @RequestMapping methods assume @ResponseBody semantics by default. In @Controller, we need to use @ResponseBody on every handler method.

What is @RestController used for?

RestController is a Spring annotation that is used to build REST API in a declarative way. RestController annotation is applied to a class to mark it as a request handler, and Spring will do the building and provide the RESTful web service at runtime.

When should I use RepositoryRestResource?

@RepositoryRestResource is used to set options on the public Repository interface - it will automatically create endpoints as appropriate based on the type of Repository that is being extended (i.e. CrudRepository/PagingAndSortingRepository/etc).

Can we use @RestController instead of @controller?

Now, you don't need to use the @Controller and the @RestponseBody annotation. Instead you can use the @RestController to provide the same functionality. In short, it is a convenience controller which combines the behavior of the @Controler and the @Response body into one.


1 Answers

According to the annotation the RepositoryRestController is a way to provide custom controllers that still take advantage of Spring Data REST functionality.

Spring Data REST Reference Guide, 15.6. Overriding Spring Data REST Response Handlers:

Sometimes you may want to write a custom handler for a specific resource. To take advantage of Spring Data REST’s settings, message converters, exception handling, and more, use the @RepositoryRestController annotation instead of a standard Spring MVC @Controller or @RestController.

Most importantly the RepositoryRestController is aware of the Spring Data REST base path and will be served under this base path.

like image 112
Mathias Dpunkt Avatar answered Oct 20 '22 00:10

Mathias Dpunkt