Java 如何混合使用Spring数据存储库和Spring Rest控制器
目前,我将一些Spring数据存储库作为RESTful服务公开,方法是使用@RepositoryRestResource注释它们,如下所示:Java 如何混合使用Spring数据存储库和Spring Rest控制器,java,spring,spring-data,spring-data-rest,spring-hateoas,Java,Spring,Spring Data,Spring Data Rest,Spring Hateoas,目前,我将一些Spring数据存储库作为RESTful服务公开,方法是使用@RepositoryRestResource注释它们,如下所示: @RepositoryRestResource(collectionResourceRel = "thing1", path = "thing1") public interface Thing1Repository extends PagingAndSortingRepository<Thing1, String> {} @Reposito
@RepositoryRestResource(collectionResourceRel = "thing1", path = "thing1")
public interface Thing1Repository extends PagingAndSortingRepository<Thing1, String> {}
@RepositoryRestResource(collectionResourceRel = "thing2", path = "thing2")
public interface Thing2Repository extends CrudRepository<Thing2, String> {}
{
_links: {
thing1: {
href: "http://localhost:8080/thing1{?page,size,sort}",
templated: true
},
thing2: {
href: "http://localhost:8080/thing2"
}
}
}
现在我有一些端点要公开,它们不能由Spring数据存储库表示,所以我使用RestController
下面是一个简单的例子:
@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;
}
}
现在,如果我运行此应用程序并转到:
http://localhost:8080/thing3/{id}
我确实得到了Thing3的JSON表示,它本身有一个链接,可以按预期工作
我想弄清楚如何让第一个端点也描述这个控制器。我基本上想要这个:
{
_links: {
thing1: {
href: "http://localhost:8080/thing1{?page,size,sort}",
templated: true
},
thing2: {
href: "http://localhost:8080/thing2"
},
thing3: {
href: "http://localhost:8080/thing3"
}
}
}
我需要做什么才能使我的基本端点具有指向此控制器的链接?您可以覆盖RepositoryLinkResource,并添加指向您的内容的资源3:
resource.add(ControllerLinkBuilder.linkTo(Thing3Controller.class).withRel("thing3"));
检查这个问题:您是如何让@RestController与spring hateoas一起工作的?此外,我还试图了解
thing3.add方法的来源。如何管理实体集合?