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

The following examples show how to use org.springframework.hateoas.Resource#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: AdResourceProcessor.java    From spring-rest-black-market with MIT License 6 votes vote down vote up
@Override
public Resource<Ad> process(Resource<Ad> resource) {
    Ad ad = resource.getContent();
    if (hasAccessToModify(ad)) {
        Ad.Status status = ad.getStatus();
        if (status == Ad.Status.NEW) {
            resource.add(entityLinks.linkForSingleResource(Ad.class, ad.getId()).withRel("update"));
            resource.add(entityLinks.linkForSingleResource(Ad.class, ad.getId()).withRel("deletion"));
            resource.add(linkTo(methodOn(AdResourceController.class).publish(ad.getId(), null)).withRel("publishing"));
        }
        if (status == Ad.Status.PUBLISHED) {
            resource.add(linkTo(methodOn(AdResourceController.class).expire(ad.getId(), null)).withRel("expiration"));
        }
    }
    return resource;
}
 
Example 2
Source File: RamlResourceSnippetIntegrationTest.java    From restdocs-raml with MIT License 5 votes vote down vote up
@PostMapping(path = "/some/{someId}/other/{otherId}")
public ResponseEntity<Resource<TestDataHolder>> doSomething(@PathVariable String someId,
                                                            @PathVariable Integer otherId,
                                                            @RequestHeader("X-Custom-Header") String customHeader,
                                                            @RequestBody TestDataHolder testDataHolder) {
    testDataHolder.setId(UUID.randomUUID().toString());
    Resource<TestDataHolder> resource = new Resource<>(testDataHolder);
    resource.add(linkTo(methodOn(TestController.class).doSomething(someId, otherId, null, null)).withSelfRel());
    resource.add(linkTo(methodOn(TestController.class).doSomething(someId, otherId, null, null)).withRel("multiple"));
    resource.add(linkTo(methodOn(TestController.class).doSomething(someId, otherId, null, null)).withRel("multiple"));
    return ResponseEntity
            .ok()
            .header("X-Custom-Header", customHeader)
            .body(resource);
}
 
Example 3
Source File: PersonController.java    From spring-boot-multitenant with Apache License 2.0 5 votes vote down vote up
/**
 * Person creation.
 * @return Person
 */
@RequestMapping(value = "/", method = RequestMethod.POST)
@ResponseBody public ResponseEntity<?> savePerson(@RequestBody final Person person) {
  final Person persistedPerson = repository.save(person);
  final Resource<Person> resource = new Resource<Person>(persistedPerson);
  resource.add(
      linkTo(methodOn(PersonController.class).getPerson(persistedPerson.getId())).withSelfRel()
  );
  return ResponseEntity
      .status(HttpStatus.CREATED)
      .contentType(MediaType.APPLICATION_JSON)
      .body(resource);
}
 
Example 4
Source File: PersonController.java    From spring-boot-multitenant with Apache License 2.0 5 votes vote down vote up
/**
 * Person retriever.
 * @return Person
 */
@RequestMapping(value = "/{personId}", method = RequestMethod.GET)
@ResponseBody public ResponseEntity<?> getPerson(@PathVariable final Long personId) {
  final Person person = repository.findOne(personId);
  if (person == null) {
    return ResponseEntity.notFound().build();
  }
  final Resource<Person> resource = new Resource<Person>(person);
  resource.add(linkTo(methodOn(PersonController.class).getPerson(personId)).withSelfRel());

  return ResponseEntity.ok(resource);
}
 
Example 5
Source File: HateoasUtil.java    From JavaWeb with Apache License 2.0 5 votes vote down vote up
private static Resource<Object> getResource(String result,Link link){
	ObjectMapper mapper = new ObjectMapper();
	ObjectNode onode = mapper.createObjectNode();
	onode.putPOJO("health", HttpStatus.OK);
	onode.putPOJO("status", HttpStatus.OK.value());
	onode.putPOJO("data", result);
	Resource<Object> resource = new Resource<Object>(onode);
	resource.add(link);
	return resource;
}
 
Example 6
Source File: PersonResourceProcessor.java    From building-microservices with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<Person> process(Resource<Person> resource) {
	String id = Long.toString(resource.getContent().getId());
	UriComponents uriComponents = ServletUriComponentsBuilder.fromCurrentContextPath()
			.path("/people/{id}/photo").buildAndExpand(id);
	String uri = uriComponents.toUriString();
	resource.add(new Link(uri, "photo"));
	return resource;
}
 
Example 7
Source File: PaymentController.java    From micro-ecommerce with Apache License 2.0 5 votes vote down vote up
/**
 * Renders the given {@link Receipt} including links to the associated
 * {@link Order} as well as a self link in case the {@link Receipt} is still
 * available.
 * 
 * @param receipt
 * @return
 */
private HttpEntity<Resource<Receipt>> createReceiptResponse(Receipt receipt) {

	Order order = receipt.getOrder();

	Resource<Receipt> resource = new Resource<Receipt>(receipt);
	resource.add(entityLinks.linkToSingleResource(order));

	if (!order.isTaken()) {
		resource.add(entityLinks.linkForSingleResource(order).slash("receipt").withSelfRel());
	}

	return new ResponseEntity<Resource<Receipt>>(resource, HttpStatus.OK);
}
 
Example 8
Source File: TaskResourceAssembler.java    From chaos-lemur with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<Task> toResource(Task task) {
    Resource<Task> resource = new Resource<>(task);

    resource.add(getTaskLinkBuilder(task).withSelfRel());

    return resource;
}
 
Example 9
Source File: TodoController.java    From Mastering-Spring-5.0 with MIT License 5 votes vote down vote up
@GetMapping(path = "/users/{name}/todos/{id}")
public Resource<Todo> retrieveTodo(@PathVariable String name, @PathVariable int id) {
	Todo todo = todoService.retrieveTodo(id);
	if (todo == null) {
		throw new TodoNotFoundException("Todo Not Found");
	}
	
	Resource<com.mastering.spring.springboot.bean.Todo> todoResource = new Resource<com.mastering.spring.springboot.bean.Todo>(todo);
	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveTodos(name));
	todoResource.add(linkTo.withRel("parent"));
	return todoResource;
}
 
Example 10
Source File: TodoController.java    From Mastering-Spring-5.0 with MIT License 5 votes vote down vote up
@GetMapping(path = "/users/{name}/todos/{id}")
public Resource<Todo> retrieveTodo(@PathVariable String name, @PathVariable int id) {
	Todo todo = todoService.retrieveTodo(id);
	if (todo == null) {
		throw new TodoNotFoundException("Todo Not Found");
	}
	
	Resource<com.mastering.spring.springboot.bean.Todo> todoResource = new Resource<com.mastering.spring.springboot.bean.Todo>(todo);
	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveTodos(name));
	todoResource.add(linkTo.withRel("parent"));
	return todoResource;
}
 
Example 11
Source File: TodoController.java    From Mastering-Spring-5.0 with MIT License 5 votes vote down vote up
@GetMapping(path = "/users/{name}/todos/{id}")
public Resource<Todo> retrieveTodo(@PathVariable String name, @PathVariable int id) {
	Todo todo = todoService.retrieveTodo(id);
	if (todo == null) {
		throw new TodoNotFoundException("Todo Not Found");
	}
	
	Resource<com.mastering.spring.springboot.bean.Todo> todoResource = new Resource<com.mastering.spring.springboot.bean.Todo>(todo);
	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveTodos(name));
	todoResource.add(linkTo.withRel("parent"));
	return todoResource;
}
 
Example 12
Source File: TodoController.java    From Mastering-Spring-5.1 with MIT License 5 votes vote down vote up
@GetMapping(path = "/users/{name}/todos/{id}")
public Resource<Todo> retrieveTodo(@PathVariable String name, @PathVariable int id) {
	Todo todo = todoService.retrieveTodo(id);
	if (todo == null) {
		throw new TodoNotFoundException("Todo Not Found");
	}

	Resource<Todo> todoResource = new Resource<com.mastering.spring.springboot.bean.Todo>(todo);
	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveTodos(name));
	todoResource.add(linkTo.withRel("parent"));
	return todoResource;
}
 
Example 13
Source File: BookmarkResourceAssembler.java    From Microservices-with-Spring-Cloud with MIT License 5 votes vote down vote up
@Override
public Resource<Bookmark> toResource(Bookmark entity) {
    Resource<Bookmark> resource = new Resource<>(entity,
            linkTo(methodOn(BookmarkController.class).getBookmark(entity.getUuid())).withSelfRel());
    if(request.isUserInRole("ADMIN")){
        resource.add(
                linkTo(methodOn(BookmarkController.class).getBookmark(entity.getUuid())).withRel("update"),
                linkTo(methodOn(BookmarkController.class).getBookmark(entity.getUuid())).withRel("delete")
        );
    }
    return resource;
}
 
Example 14
Source File: UserJPAController.java    From Spring with Apache License 2.0 5 votes vote down vote up
@GetMapping(path = "/jpa/users/{id}")
public Resource<User> retrieveUser(@PathVariable int id) {
	final User user = userJPAService.findOne(id);
	if (Objects.isNull(user)) {
		throw new UserNotFoundException("There is no user with id: " + id);
	}

	final Resource<User> resource = new Resource<>(user);
	final ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllUsers());
	resource.add(linkTo.withRel("all-users"));

	return resource;
}
 
Example 15
Source File: PersonResourceProcessor.java    From building-microservices with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<Person> process(Resource<Person> resource) {
	String id = Long.toString(resource.getContent().getId());
	UriComponents uriComponents = ServletUriComponentsBuilder.fromCurrentContextPath()
			.path("/people/{id}/photo").buildAndExpand(id);
	String uri = uriComponents.toUriString();
	resource.add(new Link(uri, "photo"));
	return resource;
}
 
Example 16
Source File: TodoController.java    From Mastering-Spring-5.1 with MIT License 5 votes vote down vote up
@GetMapping(path = "/users/{name}/todos/{id}")
public Resource<Todo> retrieveTodo(@PathVariable String name, @PathVariable int id) {
	Todo todo = todoService.retrieveTodo(id);
	if (todo == null) {
		throw new TodoNotFoundException("Todo Not Found");
	}

	Resource<Todo> todoResource = new Resource<com.mastering.spring.springboot.bean.Todo>(todo);
	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveTodos(name));
	todoResource.add(linkTo.withRel("parent"));
	return todoResource;
}
 
Example 17
Source File: TodoController.java    From Mastering-Spring-5.1 with MIT License 5 votes vote down vote up
@GetMapping(path = "/users/{name}/todos/{id}")
public Resource<Todo> retrieveTodo(@PathVariable String name, @PathVariable int id) {
	Todo todo = todoService.retrieveTodo(id);
	if (todo == null) {
		throw new TodoNotFoundException("Todo Not Found");
	}

	Resource<Todo> todoResource = new Resource<com.mastering.spring.springboot.bean.Todo>(todo);
	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveTodos(name));
	todoResource.add(linkTo.withRel("parent"));
	return todoResource;
}
 
Example 18
Source File: UserJPAResource.java    From in28minutes-spring-microservices with MIT License 4 votes vote down vote up
@GetMapping("/jpa/users/{id}")
public Resource<User> retrieveUser(@PathVariable int id) {
	Optional<User> user = userRepository.findById(id);

	if (!user.isPresent())
		throw new UserNotFoundException("id-" + id);

	// "all-users", SERVER_PATH + "/users"
	// retrieveAllUsers
	Resource<User> resource = new Resource<User>(user.get());

	ControllerLinkBuilder linkTo = linkTo(methodOn(this.getClass()).retrieveAllUsers());

	resource.add(linkTo.withRel("all-users"));

	// HATEOAS

	return resource;
}
 
Example 19
Source File: UserResource.java    From in28minutes-spring-microservices with MIT License 4 votes vote down vote up
@GetMapping("/users/{id}")
public Resource<User> retrieveUser(@PathVariable int id) {
	User user = service.findOne(id);
	
	if(user==null)
		throw new UserNotFoundException("id-"+ id);
	
	
	//"all-users", SERVER_PATH + "/users"
	//retrieveAllUsers
	Resource<User> resource = new Resource<User>(user);
	
	ControllerLinkBuilder linkTo = 
			linkTo(methodOn(this.getClass()).retrieveAllUsers());
	
	resource.add(linkTo.withRel("all-users"));
	
	//HATEOAS
	
	return resource;
}
 
Example 20
Source File: ProductAddedResourceProcessor.java    From sos with Apache License 2.0 3 votes vote down vote up
@Override
public Resource<ProductAdded> process(Resource<ProductAdded> resource) {

	ProductAdded content = resource.getContent();

	resource.add(entityLinks.linkToSingleResource(Product.class, content.getProduct().getId()));

	return resource;
}