我有一个MemberController,它有两个GetMappings,一个返回成员的分页列表,另一个返回成员。我有一个MemberModelAssembler,它覆盖toModel并返回selfRel()链接。如何使MemberModelAssembler中的toModel方法返回每个成员的分页链接?假设我不能将Pageable和PagedResourcesAssembler传递给MemberModelAssembler?
调用API/v1/member/1时的预期结果
{
"id": 1,
"phone": "85298890006",
"profileImageUrl": null,
"displayedName": "Mak",
"salutation": "MS",
"_links": {
"self": {
"href": "http://localhost:8080/api/v1/member/1"
}
*****Want to achieve this*****
"members": {
"href": "http://localhost:8080/api/v1/memberpage=0&size=20"
*****Want to achieve this*****
}
}
我的会员控制器:
@RestController
@RequestMapping("api/v1/member")
class MemberController(
private val service: MemberService,
private val assembler: MemberModelAssembler
) {
@GetMapping
fun findAll(
pageable: Pageable,
pagedResourcesAssembler: PagedResourcesAssembler<Member>
): ResponseEntity<PagedModel<EntityModel<Member>>> {
val members = service.findAll(pageable)
return ResponseEntity(pagedResourcesAssembler.toModel(members, assembler), HttpStatus.OK)
}
@GetMapping("/{id}")
fun findById(@PathVariable id: Int): ResponseEntity<EntityModel<Member>> {
val member = service.findById(id) ?: throw ItemNotFoundException(this::class.simpleName!!, id)
return ResponseEntity(assembler.toModel(member), HttpStatus.OK)
}
}
我的成员模型程序集
@Component
class MemberModelAssembler : RepresentationModelAssembler<Member, EntityModel<Member>> {
override fun toModel(member: Member) =
EntityModel.of(
member,
linkTo(methodOn(MemberController::class.java).findById(member.id)).withSelfRel(),
)
}
1条答案
按热度按时间5rgfhyps1#
我有一个答案给任何对此仍有疑问的人。虽然这有点反作用,但你可以输入第三个参数作为初始链接。