rest discoverability work
This commit is contained in:
parent
8b409e07a6
commit
6ffd867e11
@ -1,5 +1,7 @@
|
||||
package org.baeldung.web.controller;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@ -15,6 +17,7 @@ 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;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@ -47,4 +50,14 @@ public class FooController {
|
||||
eventPublisher.publishEvent(new ResourceCreated(this, request, response, idOfCreatedResource));
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
package org.baeldung.web.controller;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* Provides some constants and utility methods to build a Link Header to be stored in the {@link HttpServletResponse} object
|
||||
*/
|
||||
public final class LinkUtil {
|
||||
|
||||
private LinkUtil() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
/**
|
||||
* Creates a Link Header to be stored in the {@link HttpServletResponse} to provide Discoverability features to the user
|
||||
*
|
||||
* @param uri
|
||||
* the base uri
|
||||
* @param rel
|
||||
* the relative path
|
||||
*
|
||||
* @return the complete url
|
||||
*/
|
||||
public static String createLinkHeader(final String uri, final String rel) {
|
||||
return "<" + uri + ">; rel=\"" + rel + "\"";
|
||||
}
|
||||
|
||||
}
|
@ -25,8 +25,8 @@ class SingleResourceRetrievedDiscoverabilityListener implements ApplicationListe
|
||||
final int positionOfLastSlash = requestURL.lastIndexOf("/");
|
||||
final String uriForResourceCreation = requestURL.substring(0, positionOfLastSlash);
|
||||
|
||||
// final String linkHeaderValue = RESTURLUtil.createLinkHeader(uriForResourceCreation, "collection");
|
||||
// response.addHeader(LINK_HEADER, linkHeaderValue);
|
||||
final String linkHeaderValue = LinkUtil.createLinkHeader(uriForResourceCreation, "collection");
|
||||
response.addHeader("Link", linkHeaderValue);
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user