如何在Spring MVC控制器中应用Spring Data投影? [英] How to apply Spring Data projections in a Spring MVC controllers?

查看:92
本文介绍了如何在Spring MVC控制器中应用Spring Data投影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直接调用数据存储库方法时是否可以指定projection?这是存储库代码-请注意,我不想通过REST公开它,而是希望能够从服务或控制器中调用它:

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);
}

像上面一样直接调用findEmployeeUsers方法时,是否可以为findEmployeeUsers方法指定projection?

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

我意识到上面的代码对于某些人来说可能看起来很奇怪...可以通过REST公开存储库并将@PreAuthorize放入存储库中. Thought Controller是进行安全检查的更正确的地方-它更自然且更易于测试.

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.

那么,可以将projection东西以某种方式传递到直接称为存储库的方法中吗?

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

推荐答案

不是,尤其是根据情况,通常将预测应用于查询执行的结果.因此,它们目前被设计为选择性地应用于域类型.

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.

从最新的Spring Data Fowler发行版GA发行以来,可以在Spring MVC控制器中以编程方式使用投影基础结构.只需为SpelAwareProxyProjectionFactory声明一个Spring bean:

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);
  }
}

查看最新版本的Page如何具有map(…)方法,该方法可用于动态转换页面内容.我们使用JDK 8 lambda使用ProjectionFactory提供转换步骤.

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.

这篇关于如何在Spring MVC控制器中应用Spring Data投影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆