#1321 - Introduced MvcLink to ease Link creation from MvcUriComponentsBuilders.
We now ease Link creation from MvcUriComponentsBuilder controller method references via MvcLink.of(…).
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.hateoas.server.mvc;
|
||||
|
||||
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.*;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.LinkRelation;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
|
||||
|
||||
/**
|
||||
* Syntactic sugar to create {@link Link} instances from {@link MvcUriComponentsBuilder} invocations.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
public class MvcLink {
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
public static Link of(Object invocation, LinkRelation relation) {
|
||||
|
||||
Assert.notNull(invocation, "MethodInvocation must not be null!");
|
||||
Assert.notNull(relation, "Link relation must not be null!");
|
||||
|
||||
return Link.of(fromMethodCall(invocation).toUriString(), relation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Link} from the given lazy {@link MvcUriComponentsBuilder} invocation.
|
||||
*
|
||||
* @param invocation must not be {@literal null}.
|
||||
* @param relation must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static Link of(Supplier<Object> invocation, LinkRelation relation) {
|
||||
|
||||
Assert.notNull(invocation, "MethodInvocation must not be null!");
|
||||
Assert.notNull(relation, "Link relation must not be null!");
|
||||
|
||||
return Link.of(fromMethodCall(invocation.get()).toUriString(), relation);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user