spring-boot - 如何使用 Spring EntityLinks 创建指向 /profiles/{user-id}/job URI 的 REST 链接?
问题描述
我有一个控制器方法,它负责返回一些数据以及客户端应用程序的有用链接。
@GetMapping(value = "/{uniqueId}")
@ResponseStatus(value = HttpStatus.OK)
public HttpEntity<UserProfileMinimalDto> getUserMinimal(@PathVariable String uniqueId) {
UserProfileMinimalDto userDto = userProfileService.getProfileMinimal(uniqueId);
userDto.add(
entityLinks.linkToSingleResource(UserProfileController.class, uniqueId),
linkTo(methodOn(UserJobController.class).getUserJobs(uniqueId)).withRel(REL_EXPERIENCES)
);
另一个控制器
@RestController
@RequestMapping(PROFILES)
@ExposesResourceFor(UserJob.class)
public class UserJobController {
@PostMapping(value = "/{uniqueId}"+"/job" )
@ResponseStatus(value = HttpStatus.CREATED)
public HttpEntity<UserJob> getUserJobs(@PathVariable String uniqueId) {
System.out.println("user jobs");
return new ResponseEntity<UserJob>(new UserJob(), HttpStatus.OK);
}
}
这将链接返回给我:
"_links": {
"self": {
"href": "http://localhost:8085/api/v1/profiles/theCoder"
},
"experiences": {
"href": "http://localhost:8085/api/v1/profiles/theCoder/job"
}
}
但我想使用EntityLinks
. 正如人们所看到的,我已将UserJobController
其作为一种UserJob
资源公开,以便我可以使用它EntityLinks
所以我尝试了以下方法,但它们都没有奏效。
entityLinks.linkFor(UserJob.class, uniqueId).withRel(REL_EXPERIENCES),
entityLinks.linkFor(UserJob.class, uniqueId, "/job").withRel(REL_EXPERIENCES)
但两人都回来了
"experiences": {
"href": "http://localhost:8085/api/v1/profiles"
}
我在这里做错了什么?或者EntityLinks
不打算以这种方式使用?
解决方案
I found the right API to use. Here is one possible solution.
entityLinks.linkFor(UserJob.class, uniqueId).slash("/job").withRel(REL_EXPERIENCES)
Note: I don't want to use controller methods.
推荐阅读
- laravel - Laravel 雄辩的自我实现与 FK 和 INDEX
- powerbi - 使用 fact 和 dim 中的 key 会产生不同的结果
- javascript - 如何理解 VUEX-STORE 中的模块
- docker-swarm - 配置由 Portainer 控制的 Docker Swarm 中保留的容器数量
- python - 读取 xlsm 文件后某些列不可见:特定列的关键错误
- c# - Web API 2 中带连字符的参数
- forms - Symfony 5,表单错误 Twig\Environment::getTemplateClass()
- angular - “CampaignComponent”类型上不存在属性“onSubmit”
- payment-gateway - 支付令牌生成和存储
- java - 不确定如何使用
在链表中创建节点时