Spring - 如何对单个资源应用投影? [英] Spring - How to apply a projection on a single resource?

查看:31
本文介绍了Spring - 如何对单个资源应用投影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对名为 Institute 的实体类应用投影.

我定义了以下投影类.

@Projection(name = "instituteProjection", types = { Institute.class })公共接口 InstituteProjection {String getOrganizationName();联系 getContact();地址 getRegisteredAddress();地址 getMailingAddress();}

我遵循了 Oliver Gierke 的回答 链接 并且能够在调用 http://localhost:8080/institutes 时返回带有投影的集合资源.我通过在服务层中实现以下方法然后使用 REST 控制器调用它来做到这一点.

@Autowired私人 ProjectionFactory 投影工厂;@自动连线InstituteTypeRepository InstituteTypeRepo;@覆盖公共 PagedResources<Institute>getAllInstitutes(可分页页面){页面<?>InstituteList = InstituteRepo.findAll(page).地图(研究所 -> 投影工厂.createProjection(研究所列表投影.类,研究所));PagedResources<研究所>InstituteListPaged = pagedResourcesAssembler.toResource(instituteList);返回机构列表分页;}

现在如何在调用 http://localhost:8080/institutes/1 时将相同的投影应用于项目资源?

更新 1:

获取单个资源的控制器方法

@RequestMapping(value = "institutes/{instituteId}", method = RequestMethod.GET,产生 = MediaType.APPLICATION_JSON_VALUE)公共响应实体getInstitute(@PathVariable Long InstituteId) {Institute Institute = service.getInstitute(instituteId);返回新的响应实体(研究所,HttpStatus.OK);}

更新 2:

从存储库中获取单个资源的服务层方法

@Override公共研究所 getInstitute(Long InstituteId) {Institute Institute = InstituteRepo.findOne(instituteId);归还学院;}

解决方案

你的问题有点不清楚.您可以自动将 Spring Data 存储库公开为其余资源,即您不需要定义自己的 Spring MVC 控制器.

http://docs.spring.io/spring-data/rest/docs/current/reference/html/#install-chapter

使用 SDR 存储库,定义为摘录投影"的投影可以自动应用于收藏资源:

http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts.excerpts

但不是单一资源.对于单个资源,客户端必须通过请求参数指定所需的投影.

如果您希望将投影自动应用于单个资源,那么这可能是实现您自己的控制器的一个原因(但是从您的问题中不清楚您是否正在这样做).

对于将投影自动应用于单个资源的替代方法(即没有自定义控制器,请参见此处):

Spring Data REST:单个资源的投影表示

如果您正在创建自定义控制器以自动应用投影,则可以简单地编写一个自定义查询方法,该方法返回投影而不是完整实体,并从您的控制器/服务中调用它:

https://spring.io/blog/2016/05/03/what-s-new-in-spring-data-hopper#projections-on-repository-query-methods

基本上,通过一些配置,您可以选择自动公开为 REST 资源的 Spring Data 存储库,而无需编写任何控制器代码.

I am trying to apply a projection on an entity class called Institute.

I have the following projection class defined.

@Projection(name = "instituteProjection", types = { Institute.class })
public interface InstituteProjection {

    String getOrganizationName();

    Contact getContact();

    Address getRegisteredAddress();

    Address getMailingAddress();

}

I followed an answer by Oliver Gierke link and was able to return a collection resource with the projections when http://localhost:8080/institutes is called. I did this by implementing the following method in the service layer and then calling it using a REST controller.

@Autowired
private ProjectionFactory projectionFactory;

@Autowired
InstituteTypeRepository instituteTypeRepo;

@Override
public PagedResources<Institute> getAllInstitutes(Pageable page) {
    Page<?> instituteList = instituteRepo.findAll(page).
            map(institute -> projectionFactory.createProjection(InstituteListProjection.class, institute));
    PagedResources<Institute> instituteListPaged = pagedResourcesAssembler.toResource(instituteList);
    return instituteListPaged;

}

Now how do I apply the same projection to an item resource when http://localhost:8080/institutes/1 is called?

UPDATE 1:

Controller method to get a single resource

@RequestMapping(value = "institutes/{instituteId}", method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> getInstitute(@PathVariable Long instituteId) {
    Institute institute = service.getInstitute(instituteId);
    return new ResponseEntity<>(institute, HttpStatus.OK);
}

UPDATE 2:

Service layer method to get the single resource from the repository

@Override
public Institute getInstitute(Long instituteId) {
    Institute institute = instituteRepo.findOne(instituteId);
    return institute;
}

解决方案

Your question is somewhat unclear. You can expose Spring Data repositories as rest resources automatically i.e. you do not need to define your own Spring MVC controller.

http://docs.spring.io/spring-data/rest/docs/current/reference/html/#install-chapter

With SDR repositories, projections defined as 'excerpt projections' can be applied automatically to collection resources:

http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts.excerpts

but not to single resources. For single resources clients have to specify the required projection by means of a request parameter.

If you wanted to have a projection automatically applied to a single resource then that may be a reason for implementing your own controller (however it is unclear from your question if this is what you are doing).

For alternative means of auto applying a projection to a single resource (i.e. without a custom controller then see here):

Spring Data REST: projection representation of single resource

If you are creating a custom controller in order to auto apply a projection then it may be possible to simple write a custom query method that returns a Projection rather than the full entity and invoke that from your controller/service:

https://spring.io/blog/2016/05/03/what-s-new-in-spring-data-hopper#projections-on-repository-query-methods

Essentially then with a bit of configuration you could have selected Spring Data repositories automatically exposed as REST resources without having to write any controller code.

这篇关于Spring - 如何对单个资源应用投影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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