使用 Spring Data Rest 检索其他非数据库信息
Retrieving additional non DB information using Spring Data Rest
我正在使用 Spring Data Rest 来公开新闻提要 REST API。我想将图像(位置)添加到将由单独的 Web 服务 API 调用检索的实体。
使用 Spring Data Rest 执行此操作的最佳方法是什么,还是我必须创建另一个单独的 REST API call/domain 对象等?
任何示例代码都会很棒。
你应该使用 ResourceProcessor
The Spring Data REST exporter executes any discovered ResourceProcessor's before it creates the output representation
@Bean
public ResourceProcessor<Resource<MyEntity>> myEntityProcessor() {
return new ResourceProcessor<Resource<MyEntity>>() {
@Override
public Resource<MyEntity> process(Resource<MyEntity> resource) {
resource.add(new Link("http://localhost:8080/images/images.jpg", "image"));
return resource;
}
};
}
另一个可以访问存储库和 EntityLinks 对象的示例,有助于建立与实体相关的链接。
@Component
class MyEntityResourceProcessor implements ResourceProcessor<Resource<MyEntity>> {
@Autoware
private MyEntityRepo repo;
@Autoware
private EntityLinks entityLinks;
@Override
public Resource<MyEntity> process(Resource<MyEntity> resource) {
MyEntity entity = resource.getContent();
// Some entity processing...
Link link entityLinks.linkForSingleResource(entity).slash("...").withRel("...")
resource.add(link);
return resource;
}
}
您可以在 RESTBucks project
中找到更多使用 ResourceProcessor 的示例
我正在使用 Spring Data Rest 来公开新闻提要 REST API。我想将图像(位置)添加到将由单独的 Web 服务 API 调用检索的实体。
使用 Spring Data Rest 执行此操作的最佳方法是什么,还是我必须创建另一个单独的 REST API call/domain 对象等?
任何示例代码都会很棒。
你应该使用 ResourceProcessor
The Spring Data REST exporter executes any discovered ResourceProcessor's before it creates the output representation
@Bean
public ResourceProcessor<Resource<MyEntity>> myEntityProcessor() {
return new ResourceProcessor<Resource<MyEntity>>() {
@Override
public Resource<MyEntity> process(Resource<MyEntity> resource) {
resource.add(new Link("http://localhost:8080/images/images.jpg", "image"));
return resource;
}
};
}
另一个可以访问存储库和 EntityLinks 对象的示例,有助于建立与实体相关的链接。
@Component
class MyEntityResourceProcessor implements ResourceProcessor<Resource<MyEntity>> {
@Autoware
private MyEntityRepo repo;
@Autoware
private EntityLinks entityLinks;
@Override
public Resource<MyEntity> process(Resource<MyEntity> resource) {
MyEntity entity = resource.getContent();
// Some entity processing...
Link link entityLinks.linkForSingleResource(entity).slash("...").withRel("...")
resource.add(link);
return resource;
}
}
您可以在 RESTBucks project
中找到更多使用 ResourceProcessor 的示例