h3yon / Spring-In-Action

0 stars 0 forks source link

Chapter6 hateos 변경 사항2 #3

Open h3yon opened 2 years ago

h3yon commented 2 years ago

문제상황

SpringBoot 2.5.5 기준 DesignTacoController에서
Hyperlink 부분 ControllerLinkBuilder에서 에러

    @GetMapping("/recent")
    public CollectionModel<EntityModel<Taco>> recentTacos() {
        PageRequest page = PageRequest.of(0, 12, Sort.by("createdAt").descending());

        List<Taco> tacos = tacoRepo.findAll(page).getContent();
        CollectionModel<EntityModel<Taco>> recentResources = CollectionModel.wrap(tacos);

        // 이 부분입니다!
        recentResources.add(
                ControllerLinkBuilder
                        .linkTo(DesignTacoController.class)
                        .slash("recent")
                        .withRel("recents"));
        return recentResources;
    }
h3yon commented 2 years ago

recentResources.add 아래 부분
아래 코드와 같이 변경 후 해결

        recentResources.add(
                WebMvcLinkBuilder
                        .linkTo(WebMvcLinkBuilder.methodOn(DesignTacoController.class)
                                .recentTacos())
                        .withRel("recents"));