我已经定义了一个@Projection我的数据春春实体RepositoryRestController。当我做GET
请求时,一切都按预期返回。但是当我做POST
请求时,投影将不起作用。按照上面提供的示例,“地址”在链接下显示为URL,并未按照GET
请求的方式公开。描述<a href="http://docs.spring.io/spring-data/rest/docs/current/reference/html/#projections-excerpts.excerpting-commonly-accessed-data" rel="nofollow noreferrer">here</a></p> <p>出于同样的原因,因为描述excerptProjection
如何以同样的方式获得它?
我用@RepositoryRestController
创建了一个类,我可以在这里找到POST
方法。如果我简单地返回实体,它是没有链接的。如果我将它作为资源返回,链接就在那里,但“地址”也是一个链接。如果我从控制器中删除GET
方法,则默认行为如上所述。
UPDATE
我的实体是相同的描述hereA
,B
和除了我SuperClass
没有取我@ManyToOne
我的控制器定义看起来是这样的:
@RepositoryRestController
public class BRepositoryRestController {
private final BRepository bRepository;
public BRepositoryRestController(BRepository bRepository) {
this.bRepository = bRepository;
}
@RequestMapping(method = RequestMethod.POST, value = "/bs")
public
ResponseEntity<?> post(@RequestBody Resource<B> bResource) {
B b= bRepository.save(bResource.getContent());
BProjection result = bRepository.findById(b.getId());
return ResponseEntity.ok(new Resource<>(result));
}
}
我的资料库看起来像这样:
@RepositoryRestResource(excerptProjection = BProjection.class)
public interface BRepository extends BaseRepository<B, Long> {
@EntityGraph(attributePaths = {"a"})
BProjection findById(Long id);
}
我的投影是这样的:
@Projection(types = B.class)
public interface BProjection extends SuperClassProjection {
A getA();
String getSomeData();
String getOtherData();
}
而且SuperClassProjection看起来是这样的:
@Projection(types = SuperClass.class)
public interface SuperClassProjection {
Long getId();
}
嗯,我的仓库被延长CRUDRepository。我得到一个错误:'查询指定的连接提取,但获取的关联的所有者没有出现在选择列表中......' – JamesLinux
@JamesLinux将你的模型和回购代码添加到你的问题 - 这将是我更容易纠正我的代码。但主要想法是返回控制器方法中的投影。 – Cepr0
这和示例中的一样。我定义了一个@ManyToOne(可选= false,cascade = CascadeType.PERSIST)'。我的预测也是一样的。 – JamesLinux