Example usage for org.springframework.hateoas Links getLink

List of usage examples for org.springframework.hateoas Links getLink

Introduction

In this page you can find the example usage for org.springframework.hateoas Links getLink.

Prototype

public Optional<Link> getLink(LinkRelation rel) 

Source Link

Document

Returns the Link with the given rel.

Usage

From source file:example.springdata.rest.stores.StarbucksClient.java

@Test
public void accessServiceUsingRestTemplate() {

    // Access root resource

    URI uri = URI.create(String.format(SERVICE_URI, port));
    RequestEntity<Void> request = RequestEntity.get(uri).accept(HAL_JSON).build();
    Resource<Object> rootLinks = restOperations.exchange(request, new ResourceType<Object>() {
    }).getBody();//from  w  w  w  .  j a v  a  2 s.com
    Links links = new Links(rootLinks.getLinks());

    // Follow stores link

    Link storesLink = links.getLink("stores").expand();
    request = RequestEntity.get(URI.create(storesLink.getHref())).accept(HAL_JSON).build();
    Resources<Store> stores = restOperations.exchange(request, new ResourcesType<Store>() {
    }).getBody();

    stores.getContent().forEach(store -> log.info("{} - {}", store.name, store.address));
}

From source file:org.springframework.data.rest.webmvc.PersistentEntityResourceAssemblerIntegrationTests.java

/**
 * @see DATAREST-609//from www . j a v  a  2  s  .  c  o  m
 */
@Test
public void addsSelfAndSingleResourceLinkToResourceByDefault() throws Exception {

    Projector projector = mock(Projector.class);

    when(projector.projectExcerpt(anyObject())).thenAnswer(new ReturnsArgumentAt(0));

    PersistentEntityResourceAssembler assembler = new PersistentEntityResourceAssembler(entities, projector,
            associations,
            new DefaultSelfLinkProvider(entities, entityLinks, Collections.<EntityLookup<?>>emptyList()));

    User user = new User();
    user.id = BigInteger.valueOf(4711);

    PersistentEntityResource resource = assembler.toResource(user);

    Links links = new Links(resource.getLinks());

    assertThat(links, is(Matchers.<Link>iterableWithSize(2)));
    assertThat(links.getLink("self").getVariables(), is(Matchers.empty()));
    assertThat(links.getLink("user").getVariableNames(), is(hasItem("projection")));
}