org.springframework.hateoas.Resource.getLinks()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(73)

本文整理了Java中org.springframework.hateoas.Resource.getLinks方法的一些代码示例,展示了Resource.getLinks的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Resource.getLinks方法的具体详情如下:
包路径:org.springframework.hateoas.Resource
类名称:Resource
方法名:getLinks

Resource.getLinks介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-hateoas

private static List<CollectionJsonItem<?>> resourcesToCollectionJsonItems(Resources<?> resources) {
  
  return resources.getContent().stream()
    .map(content -> {
      if (ClassUtils.isAssignableValue(Resource.class, content)) {
        Resource resource = (Resource) content;
        return new CollectionJsonItem<>()
          .withHref(resource.getRequiredLink(IanaLinkRelation.SELF.value()).getHref())
          .withLinks(withoutSelfLink(resource.getLinks()))
          .withRawData(resource.getContent());
      } else {
        return new CollectionJsonItem<>().withRawData(content);
      }
    })
    .collect(Collectors.toList());
}

代码示例来源:origin: spring-projects/spring-hateoas

@Override
public void serialize(Resource<?> value, JsonGenerator gen, SerializerProvider provider) throws IOException {
  HalFormsDocument<?> doc = HalFormsDocument.forResource(value.getContent()) //
      .withLinks(value.getLinks()) //
      .withTemplates(findTemplates(value));
  provider
    .findValueSerializer(HalFormsDocument.class, property)
    .serialize(doc, gen, provider);
}

代码示例来源:origin: spring-projects/spring-hateoas

@Override
public void serialize(Resource<?> value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
  String href = value.getRequiredLink(IanaLinkRelation.SELF.value()).getHref();
  CollectionJson<?> collectionJson = new CollectionJson()
    .withVersion("1.0")
    .withHref(href)
    .withLinks(withoutSelfLink(value.getLinks()))
    .withItems(Collections.singletonList(new CollectionJsonItem<>()
      .withHref(href)
      .withLinks(withoutSelfLink(value.getLinks()))
      .withRawData(value.getContent())))
    .withQueries(findQueries(value))
    .withTemplate(findTemplate(value));
  CollectionJsonDocument<?> doc = new CollectionJsonDocument<>(collectionJson);
  provider
    .findValueSerializer(CollectionJsonDocument.class, property)
    .serialize(doc, jgen, provider);
}

代码示例来源:origin: spring-projects/spring-data-rest

/**
   * Creates a {@link ProjectionResource} for the given {@link TargetAware}.
   *
   * @param value must not be {@literal null}.
   * @return
   */
  private ProjectionResource toResource(TargetAware value) {
    Object target = value.getTarget();
    ResourceMetadata metadata = associations.getMetadataFor(value.getTargetClass());
    Links links = metadata.isExported() ? collector.getLinksFor(target) : new Links();
    Resource<TargetAware> resource = invoker.invokeProcessorsFor(new Resource<TargetAware>(value, links));
    return new ProjectionResource(resource.getContent(), resource.getLinks());
  }
}

代码示例来源:origin: dschulten/hydra-java

@Override
@JsonSerialize(using = LinkListSerializer.class)
@JsonUnwrapped
public List<Link> getLinks() {
  return super.getLinks();
}

代码示例来源:origin: spring-projects/spring-hateoas-examples

@GetMapping(value = "/supervisors/{id}", produces = MediaTypes.HAL_JSON_VALUE)
  public ResponseEntity<Resource<Supervisor>> findOne(@PathVariable Long id) {

    Resource<Manager> managerResource = controller.findOne(id).getBody();
    Resource<Supervisor> supervisorResource = new Resource<>(
      new Supervisor(managerResource.getContent()),
      managerResource.getLinks());

    return ResponseEntity.ok(supervisorResource);
  }
}

代码示例来源:origin: joshlong/the-spring-rest-stack

@RequestMapping(method = POST)
HttpEntity<Void> writeUserProfilePhoto(@PathVariable Long user, @RequestParam MultipartFile file) throws Throwable {
  byte bytesForProfilePhoto[] = FileCopyUtils.copyToByteArray(file.getInputStream());
  this.crmService.writeUserProfilePhoto(user, MediaType.parseMediaType(file.getContentType()), bytesForProfilePhoto);
  Resource<User> userResource = this.userResourceAssembler.toResource(crmService.findById(user));
  List<Link> linkCollection = userResource.getLinks();
  Links wrapperOfLinks = new Links(linkCollection);
  HttpHeaders httpHeaders = new HttpHeaders();
  httpHeaders.add("Link", wrapperOfLinks.toString());  // we can't encode the links in the body of the response, so we put them in the "Links:" header.
  httpHeaders.setLocation(URI.create(userResource.getLink("photo").getHref())); // "Location: /users/{userId}/photo"
  return new ResponseEntity<>(httpHeaders, HttpStatus.ACCEPTED);
}

代码示例来源:origin: joshlong/the-spring-rest-stack

@RequestMapping(method = POST)
HttpEntity<Void> writeUserProfilePhoto(@PathVariable Long user, @RequestParam MultipartFile file) throws Throwable {
  byte bytesForProfilePhoto[] = FileCopyUtils.copyToByteArray(file.getInputStream());
  this.crmService.writeUserProfilePhoto(user, MediaType.parseMediaType(file.getContentType()), bytesForProfilePhoto);
  Resource<User> userResource = this.userResourceAssembler.toResource(crmService.findById(user));
  List<Link> linkCollection = userResource.getLinks();
  Links wrapperOfLinks = new Links(linkCollection);
  HttpHeaders httpHeaders = new HttpHeaders();
  httpHeaders.add("Link", wrapperOfLinks.toString());  // we can't encode the links in the body of the response, so we put them in the "Links:" header.
  httpHeaders.setLocation(URI.create(userResource.getLink("photo").getHref())); // "Location: /users/{userId}/photo"
  return new ResponseEntity<>(httpHeaders, HttpStatus.ACCEPTED);
}

代码示例来源:origin: dschulten/hydra-java

private void traverseSingleSubEntity(SirenEntityContainer objectNode, Object content,
                   String name, String docUrl)
    throws InvocationTargetException, IllegalAccessException {
  Object bean;
  List<Link> links;
  if (content instanceof Resource) {
    bean = ((Resource) content).getContent();
    links = ((Resource) content).getLinks();
  } else if (content instanceof ResourceSupport) {
    bean = content;
    links = ((ResourceSupport) content).getLinks();
  } else {
    bean = content;
    links = Collections.emptyList();
  }
  Map<String, Object> properties = new HashMap<String, Object>();
  List<String> rels = Collections.singletonList(docUrl != null ? docUrl : name);
  SirenEmbeddedRepresentation subEntity = new SirenEmbeddedRepresentation(
      getSirenClasses(bean), properties, null, toSirenActions(getActions(links)),
      toSirenLinks(getNavigationalLinks(links)), rels, null);
  //subEntity.setProperties(properties);
  objectNode.addSubEntity(subEntity);
  List<SirenEmbeddedLink> sirenEmbeddedLinks = toSirenEmbeddedLinks(getEmbeddedLinks(links));
  for (SirenEmbeddedLink sirenEmbeddedLink : sirenEmbeddedLinks) {
    subEntity.addSubEntity(sirenEmbeddedLink);
  }
  createRecursiveSirenEntitiesFromPropertiesAndFields(subEntity, properties, bean);
}

代码示例来源:origin: de.escalon.hypermedia/spring-hateoas-ext

private void traverseSingleSubEntity(SirenEntityContainer objectNode, Object content,
                   String name, String docUrl)
    throws InvocationTargetException, IllegalAccessException {
  Object bean;
  List<Link> links;
  if (content instanceof Resource) {
    bean = ((Resource) content).getContent();
    links = ((Resource) content).getLinks();
  } else if (content instanceof ResourceSupport) {
    bean = content;
    links = ((ResourceSupport) content).getLinks();
  } else {
    bean = content;
    links = Collections.emptyList();
  }
  Map<String, Object> properties = new HashMap<String, Object>();
  List<String> rels = Collections.singletonList(docUrl != null ? docUrl : name);
  SirenEmbeddedRepresentation subEntity = new SirenEmbeddedRepresentation(
      getSirenClasses(bean), properties, null, toSirenActions(getActions(links)),
      toSirenLinks(getNavigationalLinks(links)), rels, null);
  //subEntity.setProperties(properties);
  objectNode.addSubEntity(subEntity);
  List<SirenEmbeddedLink> sirenEmbeddedLinks = toSirenEmbeddedLinks(getEmbeddedLinks(links));
  for (SirenEmbeddedLink sirenEmbeddedLink : sirenEmbeddedLinks) {
    subEntity.addSubEntity(sirenEmbeddedLink);
  }
  createRecursiveSirenEntitiesFromPropertiesAndFields(subEntity, properties, bean);
}

代码示例来源:origin: de.escalon.hypermedia/spring-hateoas-ext

Resource<?> resource = (Resource<?>) object;
if(!visitor.visitLinks(resource.getLinks())) {
  return;

代码示例来源:origin: dschulten/hydra-java

Resource<?> resource = (Resource<?>) object;
if(!visitor.visitLinks(resource.getLinks())) {
  return;

代码示例来源:origin: de.escalon.hypermedia/spring-hateoas-ext

objectNode.addLinks(resource.getLinks());
toUberData(objectNode, resource.getContent());
return;

代码示例来源:origin: dschulten/hydra-java

objectNode.addLinks(resource.getLinks());
toUberData(objectNode, resource.getContent());
return;

代码示例来源:origin: org.springframework.data/spring-data-rest-webmvc

/**
   * Creates a {@link ProjectionResource} for the given {@link TargetAware}.
   *
   * @param value must not be {@literal null}.
   * @return
   */
  private ProjectionResource toResource(TargetAware value) {
    Object target = value.getTarget();
    ResourceMetadata metadata = associations.getMetadataFor(value.getTargetClass());
    Links links = metadata.isExported() ? collector.getLinksFor(target) : new Links();
    Resource<TargetAware> resource = invoker.invokeProcessorsFor(new Resource<TargetAware>(value, links));
    return new ProjectionResource(resource.getContent(), resource.getLinks());
  }
}

代码示例来源:origin: de.escalon.hypermedia/spring-hateoas-ext

Resource<?> resource = (Resource<?>) object;
objectNode.setLinks(this.toSirenLinks(
    getNavigationalLinks(resource.getLinks())));
objectNode.setEmbeddedLinks(this.toSirenEmbeddedLinks(
    getEmbeddedLinks(resource.getLinks())));
objectNode.setActions(this.toSirenActions(getActions(resource.getLinks())));
toSirenEntity(objectNode, resource.getContent());
return;

代码示例来源:origin: dschulten/hydra-java

Resource<?> resource = (Resource<?>) object;
objectNode.setLinks(this.toSirenLinks(
    getNavigationalLinks(resource.getLinks())));
objectNode.setEmbeddedLinks(this.toSirenEmbeddedLinks(
    getEmbeddedLinks(resource.getLinks())));
objectNode.setActions(this.toSirenActions(getActions(resource.getLinks())));
toSirenEntity(objectNode, resource.getContent());
return;

代码示例来源:origin: dschulten/hydra-java

writer.writeLinks(resource.getLinks());

代码示例来源:origin: de.escalon.hypermedia/spring-hateoas-ext

writer.writeLinks(resource.getLinks());

相关文章

微信公众号

最新文章

更多