• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

EventResource 클래스 코드 공유드립니다.

20.06.20 11:50 작성 조회수 161

3

EntityModel 객체를 상속하고 EntityModel 내부를 뜯어보았을 때 of 메소드 패턴을 가지고 있고, EventResource는 Event라는 도메인에 종속된 객체이기 때문에 아래와 같이 처리하였습니다.

public class EventResource extends EntityModel<Event> {

private static WebMvcLinkBuilder selfLinkBuilder = linkTo(EventController.class);

private EventResource(){ }

public static EntityModel<Event> of(Event event, String profile){
List<Link> links = getSelfLink(event);
links.add(Link.of(profile, "profile"));
return EntityModel.of(event, links);
}

public static EntityModel<Event> of(Event event){
List<Link> links = getSelfLink(event);
return EntityModel.of(event, links);
}

private static List<Link> getSelfLink(Event event) {
selfLinkBuilder.slash(event.getId());
List<Link> links = new ArrayList<>();
links.add(selfLinkBuilder.withSelfRel());
return links;
}

public static URI getCreatedUri(Event event) {
return selfLinkBuilder.slash(event.getId()).toUri();
}
}

답변 2

·

답변을 작성해보세요.

1

감사합니다.

0

Bruce Han님의 프로필

Bruce Han

2022.11.06

생성자는 @NoArgsConstructor 라는 롬복 어노테이션을 이용하면 한 줄 정도 줄일 수 있지 않을까 생각합니다😁