From f8f78afe85084da5b937fceaabff24e3ce1b26dd Mon Sep 17 00:00:00 2001 From: Ger Roza Date: Thu, 14 Feb 2019 11:54:35 -0200 Subject: [PATCH] Moved al HATEOAS related code from spring-rest-full module to spring-boot-rest --- spring-boot-rest/README.md | 2 ++ .../web/AbstractDiscoverabilityLiveTest.java | 6 ++-- .../web/FooDiscoverabilityLiveTest.java | 8 ++--- .../baeldung/web/LiveTestSuiteLiveTest.java | 2 ++ spring-rest-full/README.md | 2 -- .../web/controller/FooController.java | 2 -- .../web/controller/RootController.java | 20 ----------- .../event/SingleResourceRetrievedEvent.java | 22 ------------ ...ourceRetrievedDiscoverabilityListener.java | 34 ------------------ .../baeldung/web/LiveTestSuiteLiveTest.java | 3 +- .../baeldung/web/util/HTTPLinkHeaderUtil.java | 36 ------------------- 11 files changed, 12 insertions(+), 125 deletions(-) rename {spring-rest-full/src/test/java/org => spring-boot-rest/src/test/java/com}/baeldung/common/web/AbstractDiscoverabilityLiveTest.java (95%) rename {spring-rest-full/src/test/java/org => spring-boot-rest/src/test/java/com}/baeldung/web/FooDiscoverabilityLiveTest.java (83%) delete mode 100644 spring-rest-full/src/main/java/org/baeldung/web/hateoas/event/SingleResourceRetrievedEvent.java delete mode 100644 spring-rest-full/src/main/java/org/baeldung/web/hateoas/listener/SingleResourceRetrievedDiscoverabilityListener.java delete mode 100644 spring-rest-full/src/test/java/org/baeldung/web/util/HTTPLinkHeaderUtil.java diff --git a/spring-boot-rest/README.md b/spring-boot-rest/README.md index 15e80ec515..3fbd21f24e 100644 --- a/spring-boot-rest/README.md +++ b/spring-boot-rest/README.md @@ -4,3 +4,5 @@ Module for the articles that are part of the Spring REST E-book: 2. [Error Handling for REST with Spring](http://www.baeldung.com/exception-handling-for-rest-with-spring) 3. [REST Pagination in Spring](http://www.baeldung.com/rest-api-pagination-in-spring) 4. [Build a REST API with Spring and Java Config](http://www.baeldung.com/building-a-restful-web-service-with-spring-and-java-based-configuration) +5. [HATEOAS for a Spring REST Service](http://www.baeldung.com/rest-api-discoverability-with-spring) +6. [REST API Discoverability and HATEOAS](http://www.baeldung.com/restful-web-service-discoverability) diff --git a/spring-rest-full/src/test/java/org/baeldung/common/web/AbstractDiscoverabilityLiveTest.java b/spring-boot-rest/src/test/java/com/baeldung/common/web/AbstractDiscoverabilityLiveTest.java similarity index 95% rename from spring-rest-full/src/test/java/org/baeldung/common/web/AbstractDiscoverabilityLiveTest.java rename to spring-boot-rest/src/test/java/com/baeldung/common/web/AbstractDiscoverabilityLiveTest.java index 96d796349a..fc581f2631 100644 --- a/spring-rest-full/src/test/java/org/baeldung/common/web/AbstractDiscoverabilityLiveTest.java +++ b/spring-boot-rest/src/test/java/com/baeldung/common/web/AbstractDiscoverabilityLiveTest.java @@ -1,4 +1,4 @@ -package org.baeldung.common.web; +package com.baeldung.common.web; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.hamcrest.Matchers.containsString; @@ -8,8 +8,8 @@ import static org.junit.Assert.assertThat; import java.io.Serializable; -import org.baeldung.persistence.model.Foo; -import org.baeldung.web.util.HTTPLinkHeaderUtil; +import com.baeldung.persistence.model.Foo; +import com.baeldung.web.util.HTTPLinkHeaderUtil; import org.hamcrest.core.AnyOf; import org.junit.Test; import org.springframework.http.MediaType; diff --git a/spring-rest-full/src/test/java/org/baeldung/web/FooDiscoverabilityLiveTest.java b/spring-boot-rest/src/test/java/com/baeldung/web/FooDiscoverabilityLiveTest.java similarity index 83% rename from spring-rest-full/src/test/java/org/baeldung/web/FooDiscoverabilityLiveTest.java rename to spring-boot-rest/src/test/java/com/baeldung/web/FooDiscoverabilityLiveTest.java index a6577e4de8..0b98edaf03 100644 --- a/spring-rest-full/src/test/java/org/baeldung/web/FooDiscoverabilityLiveTest.java +++ b/spring-boot-rest/src/test/java/com/baeldung/web/FooDiscoverabilityLiveTest.java @@ -1,10 +1,10 @@ -package org.baeldung.web; +package com.baeldung.web; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; -import org.baeldung.common.web.AbstractDiscoverabilityLiveTest; -import org.baeldung.persistence.model.Foo; -import org.baeldung.spring.ConfigIntegrationTest; +import com.baeldung.common.web.AbstractDiscoverabilityLiveTest; +import com.baeldung.persistence.model.Foo; +import com.baeldung.spring.ConfigIntegrationTest; import org.junit.runner.RunWith; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; diff --git a/spring-boot-rest/src/test/java/com/baeldung/web/LiveTestSuiteLiveTest.java b/spring-boot-rest/src/test/java/com/baeldung/web/LiveTestSuiteLiveTest.java index 1e2ddd5ec5..b7cceb9008 100644 --- a/spring-boot-rest/src/test/java/com/baeldung/web/LiveTestSuiteLiveTest.java +++ b/spring-boot-rest/src/test/java/com/baeldung/web/LiveTestSuiteLiveTest.java @@ -1,11 +1,13 @@ package com.baeldung.web; +import com.baeldung.web.FooDiscoverabilityLiveTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ // @formatter:off + FooDiscoverabilityLiveTest.class, FooLiveTest.class ,FooPageableLiveTest.class }) // diff --git a/spring-rest-full/README.md b/spring-rest-full/README.md index 5140c4b270..b8b9034a0b 100644 --- a/spring-rest-full/README.md +++ b/spring-rest-full/README.md @@ -8,8 +8,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring The "Learn Spring Security" Classes: http://github.learnspringsecurity.com ### Relevant Articles: -- [HATEOAS for a Spring REST Service](http://www.baeldung.com/rest-api-discoverability-with-spring) -- [REST API Discoverability and HATEOAS](http://www.baeldung.com/restful-web-service-discoverability) - [ETags for REST with Spring](http://www.baeldung.com/etags-for-rest-with-spring) - [Integration Testing with the Maven Cargo plugin](http://www.baeldung.com/integration-testing-with-the-maven-cargo-plugin) - [Introduction to Spring Data JPA](http://www.baeldung.com/the-persistence-layer-with-spring-data-jpa) diff --git a/spring-rest-full/src/main/java/org/baeldung/web/controller/FooController.java b/spring-rest-full/src/main/java/org/baeldung/web/controller/FooController.java index 2e4dbcacc9..9cb028bfdb 100644 --- a/spring-rest-full/src/main/java/org/baeldung/web/controller/FooController.java +++ b/spring-rest-full/src/main/java/org/baeldung/web/controller/FooController.java @@ -7,7 +7,6 @@ import javax.servlet.http.HttpServletResponse; import org.baeldung.persistence.model.Foo; import org.baeldung.persistence.service.IFooService; import org.baeldung.web.hateoas.event.ResourceCreatedEvent; -import org.baeldung.web.hateoas.event.SingleResourceRetrievedEvent; import org.baeldung.web.util.RestPreconditions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationEventPublisher; @@ -53,7 +52,6 @@ public class FooController { public Foo findById(@PathVariable("id") final Long id, final HttpServletResponse response) { final Foo resourceById = RestPreconditions.checkFound(service.findOne(id)); - eventPublisher.publishEvent(new SingleResourceRetrievedEvent(this, response)); return resourceById; } diff --git a/spring-rest-full/src/main/java/org/baeldung/web/controller/RootController.java b/spring-rest-full/src/main/java/org/baeldung/web/controller/RootController.java index e23da6420d..a66f3d1893 100644 --- a/spring-rest-full/src/main/java/org/baeldung/web/controller/RootController.java +++ b/spring-rest-full/src/main/java/org/baeldung/web/controller/RootController.java @@ -1,22 +1,14 @@ package org.baeldung.web.controller; -import java.net.URI; import java.util.Map; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import org.baeldung.web.metric.IActuatorMetricService; import org.baeldung.web.metric.IMetricService; -import org.baeldung.web.util.LinkUtil; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.util.UriTemplate; @Controller @RequestMapping(value = "/auth/") @@ -34,18 +26,6 @@ public class RootController { // API - // discover - - @RequestMapping(value = "admin", method = RequestMethod.GET) - @ResponseStatus(value = HttpStatus.NO_CONTENT) - public void adminRoot(final HttpServletRequest request, final HttpServletResponse response) { - final String rootUri = request.getRequestURL().toString(); - - final URI fooUri = new UriTemplate("{rootUri}/{resource}").expand(rootUri, "foo"); - final String linkToFoo = LinkUtil.createLinkHeader(fooUri.toASCIIString(), "collection"); - response.addHeader("Link", linkToFoo); - } - @RequestMapping(value = "/metric", method = RequestMethod.GET) @ResponseBody public Map getMetric() { diff --git a/spring-rest-full/src/main/java/org/baeldung/web/hateoas/event/SingleResourceRetrievedEvent.java b/spring-rest-full/src/main/java/org/baeldung/web/hateoas/event/SingleResourceRetrievedEvent.java deleted file mode 100644 index 0c9eb889e6..0000000000 --- a/spring-rest-full/src/main/java/org/baeldung/web/hateoas/event/SingleResourceRetrievedEvent.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.baeldung.web.hateoas.event; - -import javax.servlet.http.HttpServletResponse; - -import org.springframework.context.ApplicationEvent; - -public class SingleResourceRetrievedEvent extends ApplicationEvent { - private final HttpServletResponse response; - - public SingleResourceRetrievedEvent(final Object source, final HttpServletResponse response) { - super(source); - - this.response = response; - } - - // API - - public HttpServletResponse getResponse() { - return response; - } - -} \ No newline at end of file diff --git a/spring-rest-full/src/main/java/org/baeldung/web/hateoas/listener/SingleResourceRetrievedDiscoverabilityListener.java b/spring-rest-full/src/main/java/org/baeldung/web/hateoas/listener/SingleResourceRetrievedDiscoverabilityListener.java deleted file mode 100644 index 32407e9f2b..0000000000 --- a/spring-rest-full/src/main/java/org/baeldung/web/hateoas/listener/SingleResourceRetrievedDiscoverabilityListener.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.baeldung.web.hateoas.listener; - -import javax.servlet.http.HttpServletResponse; - -import org.baeldung.web.hateoas.event.SingleResourceRetrievedEvent; -import org.baeldung.web.util.LinkUtil; -import org.springframework.context.ApplicationListener; -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.support.ServletUriComponentsBuilder; - -import com.google.common.base.Preconditions; -import com.google.common.net.HttpHeaders; - -@Component -class SingleResourceRetrievedDiscoverabilityListener implements ApplicationListener { - - @Override - public void onApplicationEvent(final SingleResourceRetrievedEvent resourceRetrievedEvent) { - Preconditions.checkNotNull(resourceRetrievedEvent); - - final HttpServletResponse response = resourceRetrievedEvent.getResponse(); - addLinkHeaderOnSingleResourceRetrieval(response); - } - - void addLinkHeaderOnSingleResourceRetrieval(final HttpServletResponse response) { - final String requestURL = ServletUriComponentsBuilder.fromCurrentRequestUri().build().toUri().toASCIIString(); - final int positionOfLastSlash = requestURL.lastIndexOf("/"); - final String uriForResourceCreation = requestURL.substring(0, positionOfLastSlash); - - final String linkHeaderValue = LinkUtil.createLinkHeader(uriForResourceCreation, "collection"); - response.addHeader(HttpHeaders.LINK, linkHeaderValue); - } - -} \ No newline at end of file diff --git a/spring-rest-full/src/test/java/org/baeldung/web/LiveTestSuiteLiveTest.java b/spring-rest-full/src/test/java/org/baeldung/web/LiveTestSuiteLiveTest.java index da736392c4..663935e72f 100644 --- a/spring-rest-full/src/test/java/org/baeldung/web/LiveTestSuiteLiveTest.java +++ b/spring-rest-full/src/test/java/org/baeldung/web/LiveTestSuiteLiveTest.java @@ -6,8 +6,7 @@ import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ // @formatter:off - FooDiscoverabilityLiveTest.class - ,FooLiveTest.class + FooLiveTest.class }) // public class LiveTestSuiteLiveTest { diff --git a/spring-rest-full/src/test/java/org/baeldung/web/util/HTTPLinkHeaderUtil.java b/spring-rest-full/src/test/java/org/baeldung/web/util/HTTPLinkHeaderUtil.java deleted file mode 100644 index 29f1c91ca3..0000000000 --- a/spring-rest-full/src/test/java/org/baeldung/web/util/HTTPLinkHeaderUtil.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.baeldung.web.util; - -public final class HTTPLinkHeaderUtil { - - private HTTPLinkHeaderUtil() { - throw new AssertionError(); - } - - // - - public static String extractURIByRel(final String linkHeader, final String rel) { - if (linkHeader == null) { - return null; - } - - String uriWithSpecifiedRel = null; - final String[] links = linkHeader.split(", "); - String linkRelation; - for (final String link : links) { - final int positionOfSeparator = link.indexOf(';'); - linkRelation = link.substring(positionOfSeparator + 1, link.length()).trim(); - if (extractTypeOfRelation(linkRelation).equals(rel)) { - uriWithSpecifiedRel = link.substring(1, positionOfSeparator - 1); - break; - } - } - - return uriWithSpecifiedRel; - } - - private static Object extractTypeOfRelation(final String linkRelation) { - final int positionOfEquals = linkRelation.indexOf('='); - return linkRelation.substring(positionOfEquals + 2, linkRelation.length() - 1).trim(); - } - -}