我有一个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)
}
}
我的MemberModelAssembler
@Component
class MemberModelAssembler : RepresentationModelAssembler<Member, EntityModel<Member>> {
override fun toModel(member: Member) =
EntityModel.of(
member,
linkTo(methodOn(MemberController::class.java).findById(member.id)).withSelfRel(),
)
}
对于仍然有问题的人:
尽管这有点反作用,但您可以输入第三个参数作为初始链接。