Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a MemberController that has two GetMappings, one returns a paginated list of members and the other returns a member. I have a MemberModelAssembler which overrides toModel and returns a selfRel() link. How to make the toModel method in the MemberModelAssembler return a the pagination link for each member? Given I cannot pass Pageable and PagedResourcesAssembler to the MemberModelAssembler?

Expected result when calling 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*****
    }
}

My MemberController:

@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)
    }
}

My 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(),
        )
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
535 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...