How to apply Spring Data projections in a Spring MVC controllers?
Asked Answered
R

3

26

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?

Racine answered 31/3, 2015 at 18:42 Comment(0)
C
62

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.

Channel answered 1/4, 2015 at 9:12 Comment(6)
Thanks, that looks like a decent approach! Unfortunately I can not test it out as I am locked down with SpringBoot 1.2.2(3) - it does not have the laters GA version of spring-data project... I realize that this is a matter of different question - but is there way to put the GA into a SpringBoot 1.2.2 or 1.2.3 app to test this thing out?Racine
Here's how that can be done - spring.io/blog/2015/03/26/what-s-new-in-spring-data-fowler, going to try and try out the new projections infrastructure.Racine
@OliverGierke it appears that this no longer works in the current version of Spring Data? Using the example provided, Jackson throws a com.fasterxml.jackson.core.JsonGenerationException: Can not start an object, expecting field nameCavorelievo
How does transforming the results of a repository query as shown in the example above compare with using a repository query that returns a projection itself? In terms of performance, etc?Farnese
@OliverGierke is this the last improvement that I could follow or do you have some new improvement about this?Knotts
Fantastic! Now, is there a way to look up projections by name attribute from the @Projection annotation? How does the lookup work in the default JpaRepository implementations? Is this comment worth a separate question?Exo
M
4

Additionally to the @Oliver's answer, if you want to lookup the Projections by name as SpringDataRest does (instead of hardwired them in your controller), this is what you have to do:

  1. Inject RepositoryRestConfiguration into your controller. This bean gives you access to a class called ProjectionDefinitions (see, getProjectionConfiguration()) which acts a projection metadata directory.
  2. Using ProjectionDefinitions you can retrieve Projection Classes given their names and their associated bound classes.
  3. Later, you can use the method detailed by @Oliver to create the projections instances ...

This is a small Controller that implements what I describe:

@RestController
@RequestMapping("students")
public class StudentController {
    /**
     * {@link StudentController} logger.
     */
    private static final Logger logger =
            LoggerFactory.getLogger(StudentController.class);


    /**
     * Projections Factory.
     */
    private ProjectionFactory p8nFactory;

    /**
     * Projections Directory.
     */
    private ProjectionDefinitions p8nDefs;

    /**
     * {@link Student} repository.
     */
    private StudentRepository repo;

    /**
     * Class Constructor.
     *
     * @param repoConfig
     *      {@code RepositoryRestConfiguration} bean
     * @param p8nFactory
     *      Factory used to create projections
     * @param repo
     *      {@link StudentRepository} instance
     */
    @Autowired
    public StudentController(
        RepositoryRestConfiguration repoConfig, 
        ProjectionFactory p8nFactory,
        StudentRepository repo
    ) {
        super();
        this.p8nFactory = p8nFactory;
        this.p8nDefs    = repoConfig.getProjectionConfiguration();
        this.repo       = repo;
    }
    
    ...
    
    /**
     * Retrieves all persisted students.
     *
     * @param projection
     *      (Optional) Name of the projection to be applied to
     *      students retrieved from the persistence layer
     * @return
     *      {@code ResponseEntity} whose content can be a list of Students
     *      or a projected view of them
     */
    @GetMapping(path = "", produces = APPLICATION_JSON_VALUE)
    public ResponseEntity<Object> retrieveAll(
        @RequestParam(required = false) String projection
    ) {
        Class<?> type;                  // Kind of Projection to be applied
        List<?> rawData;                // Raw Entity Students
        List<?> pjData;                 // Projected students (if applies)

        rawData = this.repo.findAll();
        pjData  = rawData;

        if (projection != null) {
            type   = this.p8nDefs.getProjectionType(Student.class, projection);
            pjData = rawData
                        .stream()
                        .map(s -> this.p8nFactory.createProjection(type, s))
                        .collect(Collectors.toList());
        }
        return new ResponseEntity<>(pjData, HttpStatus.OK);
    }
}
Mielke answered 31/7, 2020 at 6:37 Comment(0)
C
2

It can be easily done in the lates Spring Data Rest releases!

All you need to do is to:

  1. pass projection name as request param

    `/api/users/search/findEmployeeUsers?projection=userView`
    
  2. return PagedModel<PersistentEntityResource> instead of Page<User> from your service method;

Done!

and I assume you want to call this service method from your custom controller, in this case you need to return ResponseEntity<PagedModel<PersistentEntityResource>> from your controller method.

Don't want it pageable? Simply return ResponseEntity<CollectionModel<PersistentEntityResource>> instead. Also check out example for single resoure projection.

Spring Data Rest takes care of applying @Projections to PersistentEntityResources on api requests, it's just like you keep exposing your @RestResource from @RepositoryRestResource; same behaviour for projections, keeping same naming convention, basically same URI (for current example).

Your service method with a bit of bussiness logic might look like:

    @Override
    @Transactional(readOnly = true)
    public PagedModel<PersistentEntityResource> listEmployees(Pageable pageable, PersistentEntityResourceAssembler resourceAssembler) {
        Page<User> users = userRepository.findEmployeeUsers(pageable);

        List<User> entities = users.getContent();
        entities.forEach(user -> user.setOnVacation(isUserOnVacationNow(user)));

        CollectionModel<PersistentEntityResource> collectionModel = resourceAssembler.toCollectionModel(entities);

        return PagedModel.of(collectionModel.getContent(), new PagedModel.PageMetadata(
                users.getSize(),
                users.getNumber(),
                users.getTotalElements(),
                users.getTotalPages()));
    }

and your controller method might look like this:

@BasePathAwareController
public class UsersController {

    @GetMapping(value = "/users/search/findEmployeeUsers")
    ResponseEntity<PagedModel<PersistentEntityResource>> findEmployeeUsers(Pageable pageable,
                                                                    PersistentEntityResourceAssembler resourceAssembler) {
        return ResponseEntity.status(HttpStatus.OK)
                .body(userService.listEmployees(pageable, resourceAssembler));
    }
}

I'm using spring-boot-starter-data-rest:2.3.4.RELEASE with spring-data-rest-webmvc:3.3.4.RELEASE and spring-data-rest-webmvc:3.3.4.RELEASE as dependencies, configuring it as parent of my pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
Concubinage answered 14/6, 2021 at 13:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.