【发布时间】:2014-06-01 21:06:03
【问题描述】:
如何在 Spring Data REST 的资源根列表中公开外部资源(不通过存储库管理)?我按照Restbucks中的模式定义了一个控制器
【问题讨论】:
标签: spring-data-rest
如何在 Spring Data REST 的资源根列表中公开外部资源(不通过存储库管理)?我按照Restbucks中的模式定义了一个控制器
【问题讨论】:
标签: spring-data-rest
这可以通过实现ResourceProcessor<RepositoryLinksResource> 来完成。
以下代码 sn-p 将“/others”添加到根列表中
@Controller
@ExposesResourceFor(Other.class)
@RequestMapping("/others")
public class CustomRootController implements
ResourceProcessor<RepositoryLinksResource> {
@ResponseBody
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<Resources<Resource<Other>>> listEntities(
Pageable pageable) throws ResourceNotFoundException {
//... do what needs to be done
}
@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add(ControllerLinkBuilder.linkTo(CustomRootController.class).withRel("others"));
return resource;
}
}
应该添加
{
"rel": "others",
"href": "http://localhost:8080/api/others"
}
到您的根列表链接
【讨论】:
@RequestMapping("/logout"),然后使用@RequestMapping(method = RequestMethod.POST) public logout(){ // do logout } 方法。非控制器链接是什么意思?
@RepositoryRestController 或@BasePathAwareController,它也错过了HAL 入口点并且无法被发现,所以linkTo 从根目录(“/”)创建。有什么解决办法吗?
我一直在寻找相同问题的答案,但关键是:我没有控制器。我的 url 指向在 auth 过滤器中创建的东西。对我有用的是创建一个没有任何方法的RootController,并将其用于在ResourceProcessor 实现中构建链接。
@RestController
@RequestMapping("/")
public class RootController {}
然后使用空控制器插入链接。
@Component
public class AuthLinkProcessor implements ResourceProcessor<RepositoryLinksResource> {
@Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add(
linkTo(RootController.class)
.slash("auth/login")
.withRel("auth-login"));
return resource;
}
}
【讨论】: