Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply Spring Data projections in a Spring MVC controllers?

Tags:

Is it possible to specify projection when calling data repository method directly? Here's repository code - note I would not like to expose it via REST, instead I would like to be able to call it from a service or controller:

@RepositoryRestResource(exported = false) public interface UsersRepository extends PagingAndSortingRepository<User, Long> {      @Query(value = "SELECT u FROM User u WHERE ....")     public Page<User> findEmployeeUsers(Pageable p); } 

Then in a controller I do this:

@PreAuthorize(value = "hasRole('ROLE_ADMIN')") @RequestMapping(value = "/users/employee") public Page<User> listEmployees(Pageable pageable) {     return usersRepository.findEmployeeUsers(pageable); } 

Is there any way to specify projection for findEmployeeUsers method when it is called directly like above?

I realise that the code above might look odd for someone... it would be possible to expose the repository via REST and put the @PreAuthorize thing in the repository. Thought controller is the more right place to do security checks - it is more natural as well as simpler to test.

So, can projection thing be somehow passed into a repository method called directly?

like image 332
Barbadoss Avatar asked Mar 31 '15 18:03

Barbadoss


People also ask

How pass data from view to controller in spring?

Spring MVC exposes a utility class called ModelMap which implicitly extends a LinkedHashMap. In order to pass data from controller to JSP, all you have to do is add a ModelMap argument to your controller method and then populate it inside your method body using the generic addAttribute() method.

What do spring data rest projections do?

Spring data REST Projection supports projecting only a selected fields from an entity representation. To do that, we can define those specific fields into our @Projection interface. Let's create a custom view of our Student entity with first name and last name fields.

What does the @RepositoryRestResource annotation do?

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

Is used for exposing spring data repositories over rest using spring data rest?

Spring Data REST can be used to expose HATEOAS RESTful resources around Spring Data repositories. Without writing a lot of code, we can expose RESTful API around Spring Data Repositories.


1 Answers

No it's not, especially as projections are usually applied to the result of a query execution on a case by case basis. Thus they're currently designed to be selectively applied to domain types.

As of the latest Spring Data Fowler release train GA release the projection infrastructure can be used programmatically in Spring MVC controllers. Simply declare a Spring bean for SpelAwareProxyProjectionFactory:

@Configuration class SomeConfig {    @Bean   public SpelAwareProxyProjectionFactory projectionFactory() {     return new SpelAwareProxyProjectionFactory();   } } 

Then inject it into your controller and use it:

@Controller class SampleController {    private final ProjectionFactory projectionFactory;    @Autowired   public SampleController(ProjectionFactory projectionFactory) {     this.projectionFactory = projectionFactory;   }    @PreAuthorize(value = "hasRole('ROLE_ADMIN')")   @RequestMapping(value = "/users/employee")   public Page<?> listEmployees(Pageable pageable) {      return usersRepository.findEmployeeUsers(pageable).//       map(user -> projectionFactory.createProjection(Projection.class, user);   } } 

See how as of the latest release Page has a map(…) method that can be used to transform the page content on the fly. We use a JDK 8 lambda to provide a conversion step using the ProjectionFactory.

like image 181
Oliver Drotbohm Avatar answered Sep 18 '22 18:09

Oliver Drotbohm