[rest api 프로젝트 구축] - spring hateous

geun kim·2023년 3월 24일
0

REST API 구축

목록 보기
7/20

hateous는 시간 날 때 공부하자....
일단, 잘 안쓰는 듯 한데...

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-hateoas</artifactId>
			<version>2.4.0</version>
		</dependency>
public class EventResource extends EntityModel<Event> {

    public EventResource(Event event, Link... links) {
        super(event, links);
        add(linkTo(EventContoller.class).slash(event.getId()).withSelfRel());
    }
}

Controller

		 /**********************************
         * hateous 작성 -- 정말 hate 이구만...
         *********************************/
        WebMvcLinkBuilder selfLinkBuilder  = linkTo(EventContoller.class).slash(newEvent.getId());
        URI createdUri = selfLinkBuilder.toUri();
        EventResource eventResource = new EventResource(event);
        eventResource.add(linkTo(EventContoller.class).withRel("query-events"));
        eventResource.add(selfLinkBuilder.withRel("update-event"));
        eventResource.add(new Link("/docs/index.html#resources-events-create").withRel("profile"));
        return ResponseEntity.created(createdUri).body(eventResource);

Test class

				/*******************************************
                 * Hateous 추가
                 ********************************************/
                .andExpect(jsonPath("_links.self").exists())
                .andExpect(jsonPath("_links.query-events").exists())
                .andExpect(jsonPath("_links.update-event").exists())
profile
Devops Load Map

0개의 댓글