Moved al HATEOAS related code from spring-rest-full module to spring-boot-rest
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
-22
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
-34
@@ -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<SingleResourceRetrievedEvent> {
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user