#1424 - More syntactic sugar for MvcLink.

This commit is contained in:
Oliver Drotbohm
2020-12-22 14:40:15 +01:00
parent 0cfbd7b414
commit 7a86ea4b31
2 changed files with 42 additions and 3 deletions

View File

@@ -19,6 +19,7 @@ import static org.springframework.web.servlet.mvc.method.annotation.MvcUriCompon
import java.util.function.Supplier;
import org.springframework.hateoas.IanaLinkRelations;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.LinkRelation;
import org.springframework.util.Assert;
@@ -31,12 +32,36 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
*/
public class MvcLink {
/**
* Creates a new {@link Link} from the given {@link MvcUriComponentsBuilder} invocation defaulting to the
* {@link IanaLinkRelations#SELF} link relation.
*
* @param invocation must not be {@literal null}.
* @return will never be {@literal null}.
* @since 1.3
*/
public static Link of(Object invocation) {
return of(invocation, IanaLinkRelations.SELF);
}
/**
* Creates a new {@link Link} from the given lazy {@link MvcUriComponentsBuilder} invocation defaulting to the
* {@link IanaLinkRelations#SELF} link relation.
*
* @param invocation must not be {@literal null}.
* @return will never be {@literal null}.
* @since 1.3
*/
public static Link of(Supplier<Object> invocation) {
return of(invocation, IanaLinkRelations.SELF);
}
/**
* Creates a new {@link Link} from the given {@link MvcUriComponentsBuilder} invocation.
*
* @param invocation must not be {@literal null}.
* @param relation must not be {@literal null}.
* @return
* @return will never be {@literal null}.
*/
public static Link of(Object invocation, LinkRelation relation) {
@@ -51,7 +76,7 @@ public class MvcLink {
*
* @param invocation must not be {@literal null}.
* @param relation must not be {@literal null}.
* @return
* @return will never be {@literal null}.
*/
public static Link of(Supplier<Object> invocation, LinkRelation relation) {
@@ -60,4 +85,18 @@ public class MvcLink {
return Link.of(fromMethodCall(invocation.get()).toUriString(), relation);
}
/**
* Syntactic sugar for {@link MvcUriComponentsBuilder#on(Class)} to avoid the additional static import.
*
* @param controller must not be {@literal null}.
* @return will never be {@literal null}.
* @since 1.3
*/
public static <T> T on(Class<T> controller) {
Assert.notNull(controller, "Controller must not be null!");
return MvcUriComponentsBuilder.on(controller);
}
}