diff --git a/src/main/java/org/springframework/hateoas/server/mvc/MvcLink.java b/src/main/java/org/springframework/hateoas/server/mvc/MvcLink.java index 3d85906a..8815b688 100644 --- a/src/main/java/org/springframework/hateoas/server/mvc/MvcLink.java +++ b/src/main/java/org/springframework/hateoas/server/mvc/MvcLink.java @@ -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 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 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 on(Class controller) { + + Assert.notNull(controller, "Controller must not be null!"); + + return MvcUriComponentsBuilder.on(controller); + } } diff --git a/src/test/java/org/springframework/hateoas/server/mvc/MvcLinkUnitTests.java b/src/test/java/org/springframework/hateoas/server/mvc/MvcLinkUnitTests.java index 1cdd4ee1..d5016cde 100644 --- a/src/test/java/org/springframework/hateoas/server/mvc/MvcLinkUnitTests.java +++ b/src/test/java/org/springframework/hateoas/server/mvc/MvcLinkUnitTests.java @@ -16,7 +16,7 @@ package org.springframework.hateoas.server.mvc; import static org.assertj.core.api.Assertions.*; -import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.*; +import static org.springframework.hateoas.server.mvc.MvcLink.*; import org.junit.jupiter.api.Test; import org.springframework.hateoas.IanaLinkRelations;