본문 바로가기
Spring Framework

스프링 부트에서 HATEOAS 를 적용하는 2가지 방법

by 자유코딩 2019. 2. 8.

1. ResourceSupport 를 사용하는 방법


public class EventResource extends ResourceSupport { // Hateoas 적용에서 추가된 코드 ( HAL 적용 )
//@JsonUnwrapped // 있으면 리턴 값이 아래와 같다. - JsonUnwrapped 를 쓴게 파싱하기 더 편하다
//{"id":1,"name":"Spring","description":"REST API Development with Spring","beginEnrollmentDateTime":"2018-11-23T14:21:00","closeEnrollmentDateTime":"2018-11-24T14:21:00","beginEventDateTime":"2018-11-25T14:21:00","endEventDateTime":"2018-11-26T14:21:00","location":"강남역 D2 스타텁 팩토리","basePrice":100,"maxPrice":200,"limitOfEnrollment":100,"offline":true,"free":false,"eventStatus":"DRAFT","_links":{"query-events":{"href":"http://localhost/api/events"},"self":{"href":"http://localhost/api/events/1"},"update-event":{"href":"http://localhost/api/events/1"}}}
//없으면 아래와 같다.
//{"event":{"id":1,"name":"Spring","description":"REST API Development with Spring","beginEnrollmentDateTime":"2018-11-23T14:21:00","closeEnrollmentDateTime":"2018-11-24T14:21:00","beginEventDateTime":"2018-11-25T14:21:00","endEventDateTime":"2018-11-26T14:21:00","location":"강남역 D2 스타텁 팩토리","basePrice":100,"maxPrice":200,"limitOfEnrollment":100,"offline":true,"free":false,"eventStatus":"DRAFT"},"_links":{"query-events":{"href":"http://localhost/api/events"},"self":{"href":"http://localhost/api/events/1"},"update-event":{"href":"http://localhost/api/events/1"}}}
private Event event;

public EventResource(Event event){
this.event = event;
}

public Event getEvent() {
return event;
} // 이벤트 getter
}

원래VO이름Resource  클래스를 만든다.


ResourceSupport를 상속 받는다.


아래 코드는 컨트롤러의 일부이다. EventResource 클래스를 사용한다.


Event event = modelMapper.map(eventDto , Event.class); // 위에 사용하지 않는 방법은 많은 값을 입력한다. //ModelMapper 를 사용하면 이 1줄로 들어온 모든 값을 1세팅 할 수 있다.
event.update();
Event newEvent = eventRepository.save(event);
ControllerLinkBuilder selfLinkBuilder = linkTo(EventController.class).slash(newEvent.getId());

URI createUri = linkTo(EventController.class).slash(newEvent.getId()).toUri();

EventResource eventResource = new EventResource(newEvent); // 링크를 추가하기 위한 코드
eventResource.add(linkTo(EventController.class).withRel("query-events"));
eventResource.add(selfLinkBuilder.withSelfRel());
eventResource.add(selfLinkBuilder.withRel("update-event"));

이렇게 eventResource에 링크를 추가하고


return ResponseEntity.ok().body(eventResource);

eventResource를 리턴하면 된다.



2. Resource<T> 를 사용하는 방법


public class EventResource extends Resource<Event> { // Hateoas 적용에서 추가된 코드 ( HAL 적용 )
//@JsonUnwrapped // 있으면 리턴 값이 아래와 같다. - JsonUnwrapped 를 쓴게 파싱하기 더 편하다
//{"id":1,"name":"Spring","description":"REST API Development with Spring","beginEnrollmentDateTime":"2018-11-23T14:21:00","closeEnrollmentDateTime":"2018-11-24T14:21:00","beginEventDateTime":"2018-11-25T14:21:00","endEventDateTime":"2018-11-26T14:21:00","location":"강남역 D2 스타텁 팩토리","basePrice":100,"maxPrice":200,"limitOfEnrollment":100,"offline":true,"free":false,"eventStatus":"DRAFT","_links":{"query-events":{"href":"http://localhost/api/events"},"self":{"href":"http://localhost/api/events/1"},"update-event":{"href":"http://localhost/api/events/1"}}}
//없으면 아래와 같다.
//{"event":{"id":1,"name":"Spring","description":"REST API Development with Spring","beginEnrollmentDateTime":"2018-11-23T14:21:00","closeEnrollmentDateTime":"2018-11-24T14:21:00","beginEventDateTime":"2018-11-25T14:21:00","endEventDateTime":"2018-11-26T14:21:00","location":"강남역 D2 스타텁 팩토리","basePrice":100,"maxPrice":200,"limitOfEnrollment":100,"offline":true,"free":false,"eventStatus":"DRAFT"},"_links":{"query-events":{"href":"http://localhost/api/events"},"self":{"href":"http://localhost/api/events/1"},"update-event":{"href":"http://localhost/api/events/1"}}}

public EventResource(Event event, Link... links){ // 이렇게 작성하면 자동으로 JsonWrapped 가 적용 된 상태로 만들어진다.
super(event, links);
add(linkTo(EventController.class).slash(event.getId()).withSelfRel());
//지금 컨트롤러에 있는 링크 추가하는 부분은 여기로 와야한다.
}
}

컨트롤러에서 link를 추가하기 위해 작성한 부분은 EventResource 코드의 생성자로 오면 된다.


그러면 생성자를 호출 했을때 링크가 생성된다.


댓글