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?
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.
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.
@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).
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With