【发布时间】:2017-08-07 12:25:34
【问题描述】:
我已经为我的 Spring Data 实体定义了一个 @Projection,如 here 所述
出于与此处所述相同的原因。当我提出GET 请求时,一切都按预期返回。但是当我发出POST 请求时,投影将不起作用。按照上面提供的示例,“地址”在链接下显示为 URL,并且不会像 GET 请求那样公开。
如何让它以同样的方式暴露出来?
我用@RepositoryRestController 创建了一个类,我可以在其中捕获POST 方法。如果我只是返回实体,它就没有链接。如果我将它作为资源返回,链接就在那里,但“地址”也是一个链接。如果我从控制器中删除 GET 方法,默认行为如上所述。
更新
我的实体与here A、B 和 SuperClass 描述的相同,但我的 @ManyToOne 中没有定义 fetch
我的控制器如下所示:
@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();
}
【问题讨论】:
标签: spring spring-data entity spring-data-jpa hateoas