From 5dc1392fac73976dc8a9084867845c38b4bc10bd Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 14 Nov 2019 09:54:24 +0400 Subject: [PATCH] hateoas showcase --- build.gradle | 4 ++- .../paper/controller/PaperRestController.java | 26 +++++++++++++++++++ .../java/ru/ulstu/paper/model/PaperDto.java | 17 ++++++------ .../ru/ulstu/paper/service/PaperService.java | 2 +- 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/build.gradle b/build.gradle index 4fb0185..0c350e4 100644 --- a/build.gradle +++ b/build.gradle @@ -105,6 +105,8 @@ dependencies { compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty' compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa' compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf' + compile group: 'org.springframework.boot', name: 'spring-boot-starter-hateoas', version: '2.2.1.RELEASE' + compile group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect' compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity5' compile group: 'com.fasterxml.jackson.module', name: 'jackson-module-afterburner' @@ -132,4 +134,4 @@ dependencies { testCompile group: 'org.seleniumhq.selenium', name: 'selenium-support', version: '3.3.1' //testCompile group: 'com.google.guava', name: 'guava', version: '21.0' testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19' -} \ No newline at end of file +} diff --git a/src/main/java/ru/ulstu/paper/controller/PaperRestController.java b/src/main/java/ru/ulstu/paper/controller/PaperRestController.java index b6dd11d..8be3099 100644 --- a/src/main/java/ru/ulstu/paper/controller/PaperRestController.java +++ b/src/main/java/ru/ulstu/paper/controller/PaperRestController.java @@ -1,5 +1,8 @@ package ru.ulstu.paper.controller; +import org.springframework.hateoas.Link; +import org.springframework.hateoas.Resource; +import org.springframework.hateoas.Resources; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -20,6 +23,8 @@ import javax.validation.Valid; import java.io.IOException; import java.util.List; +import static org.springframework.hateoas.core.DummyInvocationUtils.methodOn; +import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo; import static ru.ulstu.paper.controller.PaperRestController.URL; @RestController @@ -38,6 +43,27 @@ public class PaperRestController { return new Response<>(paperService.findAllDto()); } + @GetMapping("/with-resources") + public Resources> getPapersWithResources() { + List paperDtos = paperService.findAllDto(); + for (final PaperDto paperDto : paperDtos) { + final Link selfLink = linkTo( + methodOn(PaperRestController.class).getPaperWithResources(paperDto.getEntityId())).withSelfRel(); + paperDto.add(selfLink); + } + + Link link = linkTo(PaperRestController.class).withSelfRel(); + Resources> result = new Resources(paperDtos, link); + return result; + } + + @GetMapping("/with-resources/{paper-id}") + public Resource getPaperWithResources(@PathVariable("paper-id") Integer paperId) { + Link link = linkTo(methodOn(PaperRestController.class).getPapersWithResources()).withSelfRel(); + Resource result = new Resource(paperService.findById(paperId), link); + return result; + } + @GetMapping("/{paper-id}") public Response getPaper(@PathVariable("paper-id") Integer paperId) { return new Response<>(paperService.findById(paperId)); diff --git a/src/main/java/ru/ulstu/paper/model/PaperDto.java b/src/main/java/ru/ulstu/paper/model/PaperDto.java index cc44af5..de46e7c 100644 --- a/src/main/java/ru/ulstu/paper/model/PaperDto.java +++ b/src/main/java/ru/ulstu/paper/model/PaperDto.java @@ -3,6 +3,7 @@ package ru.ulstu.paper.model; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import org.apache.commons.lang3.StringUtils; +import org.springframework.hateoas.ResourceSupport; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.file.model.FileDataDto; import ru.ulstu.user.model.UserDto; @@ -17,10 +18,10 @@ import java.util.stream.Collectors; import static ru.ulstu.core.util.StreamApiUtils.convert; -public class PaperDto { +public class PaperDto extends ResourceSupport { private final static int MAX_AUTHORS_LENGTH = 60; - private Integer id; + private Integer entityId; @NotEmpty @Size(min = 3, max = 254) private String title; @@ -46,7 +47,7 @@ public class PaperDto { } @JsonCreator - public PaperDto(@JsonProperty("id") Integer id, + public PaperDto(@JsonProperty("entityId") Integer entityId, @JsonProperty("title") String title, @JsonProperty("status") Paper.PaperStatus status, @JsonProperty("type") Paper.PaperType type, @@ -62,7 +63,7 @@ public class PaperDto { @JsonProperty("authors") Set authors, @JsonProperty("references") List references, @JsonProperty("formatStandard") ReferenceDto.FormatStandard formatStandard) { - this.id = id; + this.entityId = entityId; this.title = title; this.status = status; this.type = type; @@ -80,7 +81,7 @@ public class PaperDto { } public PaperDto(Paper paper) { - this.id = paper.getId(); + this.entityId = paper.getId(); this.title = paper.getTitle(); this.status = paper.getStatus(); this.type = paper.getType(); @@ -97,12 +98,12 @@ public class PaperDto { this.references = convert(paper.getReferences(), ReferenceDto::new); } - public Integer getId() { - return id; + public Integer getEntityId() { + return entityId; } public void setId(Integer id) { - this.id = id; + this.entityId = id; } public String getTitle() { diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 2fee949..812f449 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -176,7 +176,7 @@ public class PaperService { @Transactional public Integer update(PaperDto paperDto) throws IOException { - Paper paper = paperRepository.getOne(paperDto.getId()); + Paper paper = paperRepository.getOne(paperDto.getEntityId()); Paper.PaperStatus oldStatus = paper.getStatus(); Set oldAuthors = new HashSet<>(paper.getAuthors()); -- 2.25.1