java 如何混合 Spring Data Repositories 和 Spring Rest Controllers
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27387856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to mix Spring Data Repositories and Spring Rest Controllers
提问by dspiegs
Currently I am exposing a few Spring Data Repositories as RESTful services by annotating them with @RepositoryRestResource like this:
目前,我通过使用 @RepositoryRestResource 注释它们来将一些 Spring Data Repositories 作为 RESTful 服务公开,如下所示:
@RepositoryRestResource(collectionResourceRel = "thing1", path = "thing1")
public interface Thing1Repository extends PagingAndSortingRepository<Thing1, String> {}
@RepositoryRestResource(collectionResourceRel = "thing2", path = "thing2")
public interface Thing2Repository extends CrudRepository<Thing2, String> {}
This all works great. When you hit my first endpoint is also shows all the Spring Data Repositories I have exposed, like this:
这一切都很好。当您点击我的第一个端点时,还会显示我公开的所有 Spring Data Repositories,如下所示:
{
_links: {
thing1: {
href: "http://localhost:8080/thing1{?page,size,sort}",
templated: true
},
thing2: {
href: "http://localhost:8080/thing2"
}
}
}
Now I have some endpoints I want to expose that cannot be represented by Spring Data Repositories, so I am using a RestController.
现在我想公开一些无法由 Spring Data Repositories 表示的端点,因此我使用了 RestController。
Here is a simple example:
这是一个简单的例子:
@RestController
@ExposesResourceFor(Thing3.class)
@RequestMapping("/thing3")
public class Thing3Controller {
@Autowired
EntityLinks entityLinks;
@Autowired
Thing3DAO thing3DAO;
//just assume Thing3.class extends ResourceSupport. I know this is wrong, but it makes the example shorter
@RequestMapping(value = "/{id}", produces = "application/json")
Thing3 thing3(@PathVariable("id") String id)
{
Thing3 thing3 = thing3DAO.findOne(id);
Link link = entityLinks.linkToSingleResource(Thing3.class, id);
thing3.add(link);
return thing3;
}
}
Now if I run this app and go to:
现在,如果我运行此应用程序并转到:
http://localhost:8080/thing3/{id}
I do get a JSON representation of the Thing3 with a link to itself, that works as expected.
我确实得到了 Thing3 的 JSON 表示,并带有指向自身的链接,这按预期工作。
What I want to figure out how to do is have the first endpoint also describe this controller. I basically want this:
我想弄清楚如何做的是让第一个端点也描述这个控制器。我基本上想要这个:
{
_links: {
thing1: {
href: "http://localhost:8080/thing1{?page,size,sort}",
templated: true
},
thing2: {
href: "http://localhost:8080/thing2"
},
thing3: {
href: "http://localhost:8080/thing3"
}
}
}
What do I need to do to get my base endpoint to have a link to this controller?
我需要做什么才能让我的基本端点链接到这个控制器?
采纳答案by Leandro Carracedo
You could override RepositoryLinkResource, and add a resource pointing to your thing3:
您可以覆盖 RepositoryLinkResource,并添加一个指向您的 thing3 的资源:
resource.add(ControllerLinkBuilder.linkTo(Thing3Controller.class).withRel("thing3"));
Check this question: Custom response for root request int the Spring REST HATEOAS with both RepositoryRestResource-s and regular controllers