#18 - Added support for creating links from method mappings.

ControllerLinkBuilder(Factory) now has a linkTo(Method method, Object… parameters) and linkTo(Object dummyMethodInvocationResult) method to either inspect the given method or the result of the dummy method invocation that can be created through DummyInvocationUtils.methodOn(…). So for the following controller:

@Controller
@RequestMapping("/people")
class PersonController {
  
  @RequestMapping(value = "/{person}", method = RequestMethod.GET)
  public HttpEntity<PersonResource> show(@PathVariable Long person) { … }
}

you could now do:

Link link = linkTo(methodOn(PersonController.class).show(2L)).withSelfRel();
assertThat(link.getHref(), is("/people/2")));

The code is highly inspired by the code Dietrich Schulten (@dschulten) provided in his pull requests but radically reduced to the core functionality. I also didn't add support for JAX-RS in the first place, but this is definitely a topic going forward.
This commit is contained in:
Oliver Gierke
2012-12-19 13:10:12 +01:00
parent 220366b240
commit 531dd8f5ed
15 changed files with 487 additions and 123 deletions

View File

@@ -18,7 +18,9 @@ package org.springframework.hateoas.jaxrs;
import javax.ws.rs.Path;
import org.springframework.hateoas.LinkBuilder;
import org.springframework.hateoas.mvc.UriComponentsLinkBuilder;
import org.springframework.hateoas.core.AnnotationMappingDiscoverer;
import org.springframework.hateoas.core.LinkBuilderSupport;
import org.springframework.hateoas.core.MappingDiscoverer;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriTemplate;
@@ -28,7 +30,9 @@ import org.springframework.web.util.UriTemplate;
*
* @author Oliver Gierke
*/
public class JaxRsLinkBuilder extends UriComponentsLinkBuilder<JaxRsLinkBuilder> {
public class JaxRsLinkBuilder extends LinkBuilderSupport<JaxRsLinkBuilder> {
private static final MappingDiscoverer DISCOVERER = new AnnotationMappingDiscoverer(Path.class);
/**
* Creates a new {@link JaxRsLinkBuilder} from the given {@link UriComponentsBuilder}.