#1424 - More syntactic sugar for MvcLink.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user