如何在 Spring MVC 控制器中应用 Spring Data 投影? [英] How to apply Spring Data projections in a Spring MVC controllers?
问题描述
直接调用数据仓库方法时是否可以指定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
方法被直接调用时,有没有办法指定projection
?
Is there any way to specify projection
for findEmployeeUsers
method when it is called directly like above?
我意识到上面的代码对于某些人来说可能看起来很奇怪……可以通过 REST 公开存储库并将 @PreAuthorize
东西放在存储库中.思想控制器更适合进行安全检查 - 它更自然,也更易于测试.
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屋!