• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

Resource HateOAS

21.10.12 15:06 작성 조회수 237

1

 

Resource 부분에서 HateOAS가 보이지 않는데 어떤 오류일까요..?

 

POM.xml 에는 정상적으로 등록되어 있습니다..

 

답변 3

·

답변을 작성해보세요.

4

nina2439님의 프로필

nina2439

2021.11.01

저는 스프링부트 2.4.12를 사용하고 있고, 처음에 Hateos 관련 클래스를 불러올 때 작성자님과 동일하게 고민했어서 도움이 되시길 바래서 강사님은 아니지만, 댓글 남깁니다.

Hateoas는 버전에 따라 사용하는 방법이 다르다고 알고있습니다. 
제 코드입니다.

@GetMapping("/users/{id}")
    public EntityModel<User> retrieveUser(@PathVariable int id){
        Optional<User> user = userRepository.findById(id);

        if(!user.isPresent()) {
            throw new UserNotFoundException(String.format("ID[%S] not found", id));
        }

        EntityModel<User> userModel = new EntityModel<>(user.get());
        WebMvcLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllUsers());
        userModel.add(linkTo.withRel("all-users"));

        return userModel;
    }



제가 작성할 때 참고한 사이트는 다음과 같습니다. https://jeonghoon.netlify.app/Spring/SpringBoot13-hateoas/ 
해결이 안되셨다면 참고하시면 좋을 것 같아요!

3

yonhasky님의 프로필

yonhasky

2022.02.06

버전문제로 이전강의 때 수정했던 내용 참조하여 아래와 같이 만들었습니다.

@GetMapping("/users/{id}")

public EntityModel<User> retrieveUser(@PathVariable int id) {

Optional<User> user = userRepository.findById(id);

if (!user.isPresent()) {

throw new UserNotFoundException(String.format("ID[%s] not found", id));

}

EntityModel<User> model = EntityModel.of(user.get());

WebMvcLinkBuilder linkTo = WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(this.getClass()).retrieveAllUsers());

model.add(linkTo.withRel("all-users"));

return model;

}

0

bbmgom님의 프로필

bbmgom

질문자

2021.10.12

이것도 스프링 버전의 문제일까요? 현재 사용하는 스프링은 2.5.5 입니다.