#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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.hateoas.IanaLinkRelations;
|
||||
import org.springframework.hateoas.TestUtils;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MvcLink}.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
class MvcLinkUnitTests extends TestUtils {
|
||||
|
||||
@Test
|
||||
void createsLinkPointingToController() {
|
||||
|
||||
Object invocation = on(MyController.class).get("4711");
|
||||
String expected = "http://localhost/4711";
|
||||
|
||||
assertThat(MvcLink.of(invocation, IanaLinkRelations.SELF).getHref()).isEqualTo(expected);
|
||||
assertThat(MvcLink.of(() -> invocation, IanaLinkRelations.SELF).getHref()).isEqualTo(expected);
|
||||
}
|
||||
|
||||
static class MyController {
|
||||
|
||||
@RequestMapping("/{id}")
|
||||
HttpEntity<?> get(@PathVariable String id) {
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user