Java Code Examples for org.springframework.hateoas.RepresentationModel#add()

The following examples show how to use org.springframework.hateoas.RepresentationModel#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageLinksAspect.java    From taskana with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Around("@annotation(pro.taskana.resource.rest.PageLinks) && args(data, page, ..)")
public <T extends RepresentationModel<? extends T> & ProceedingJoinPoint>
    RepresentationModel<T> addLinksToPageResource(
        ProceedingJoinPoint joinPoint, List<?> data, PageMetadata page) throws Throwable {
  HttpServletRequest request =
      ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
  Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
  PageLinks pageLinks = method.getAnnotation(PageLinks.class);
  String relativeUrl = pageLinks.value();
  UriComponentsBuilder original = originalUri(relativeUrl, request);
  RepresentationModel<T> resourceSupport = (RepresentationModel<T>) joinPoint.proceed();
  resourceSupport.add(Link.of(original.toUriString()).withSelfRel());
  if (page != null) {
    resourceSupport.add(
        Link.of(original.replaceQueryParam("page", 1).toUriString())
            .withRel(IanaLinkRelations.FIRST));
    resourceSupport.add(
        Link.of(original.replaceQueryParam("page", page.getTotalPages()).toUriString())
            .withRel(IanaLinkRelations.LAST));
    if (page.getNumber() > 1) {
      resourceSupport.add(
          Link.of(original.replaceQueryParam("page", page.getNumber() - 1).toUriString())
              .withRel(IanaLinkRelations.PREV));
    }
    if (page.getNumber() < page.getTotalPages()) {
      resourceSupport.add(
          Link.of(original.replaceQueryParam("page", page.getNumber() + 1).toUriString())
              .withRel(IanaLinkRelations.NEXT));
    }
  }
  return resourceSupport;
}
 
Example 2
Source File: RootController.java    From spring-hateoas-examples with Apache License 2.0 5 votes vote down vote up
@GetMapping("/")
ResponseEntity<RepresentationModel> root() {

	RepresentationModel model = new RepresentationModel();

	model.add(linkTo(methodOn(RootController.class).root()).withSelfRel());
	model.add(linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees"));
	model.add(linkTo(methodOn(EmployeeController.class).findAllDetailedEmployees()).withRel("detailedEmployees"));
	model.add(linkTo(methodOn(ManagerController.class).findAll()).withRel("managers"));

	return ResponseEntity.ok(model);
}
 
Example 3
Source File: EmployeeController.java    From spring-hateoas-examples with Apache License 2.0 5 votes vote down vote up
@GetMapping("/")
public RepresentationModel root() {

	RepresentationModel rootResource = new RepresentationModel();

	rootResource.add( //
			linkTo(methodOn(EmployeeController.class).root()).withSelfRel(), //
			linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees"));

	return rootResource;
}
 
Example 4
Source File: EmployeeController.java    From spring-hateoas-examples with Apache License 2.0 5 votes vote down vote up
@GetMapping("/")
public RepresentationModel root() {

	RepresentationModel rootResource = new RepresentationModel();

	rootResource.add( //
			linkTo(methodOn(EmployeeController.class).root()).withSelfRel(), //
			linkTo(methodOn(EmployeeController.class).findAll()).withRel("employees"));

	return rootResource;
}