tdudzik

Untitled

Jan 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. package com.epam.bazaar.backend.api.endpoints;
  2.  
  3. import com.epam.bazaar.backend.api.models.Advertisement;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.hateoas.EntityLinks;
  6. import org.springframework.hateoas.ExposesResourceFor;
  7. import org.springframework.hateoas.Resources;
  8. import org.springframework.http.HttpEntity;
  9. import org.springframework.http.HttpStatus;
  10. import org.springframework.http.MediaType;
  11. import org.springframework.http.ResponseEntity;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.web.bind.annotation.GetMapping;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15.  
  16. import java.util.Arrays;
  17. import java.util.List;
  18.  
  19. import static com.google.common.base.Preconditions.checkNotNull;
  20.  
  21.  
  22. @Controller
  23. @RequestMapping(
  24.         path = "/advertisements",
  25.         consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
  26.         produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  27. @ExposesResourceFor(Advertisement.class)
  28. public class AdvertisementEndpoint {
  29.  
  30.     private final EntityLinks entityLinks;
  31.  
  32.     @Autowired
  33.     public AdvertisementEndpoint(EntityLinks entityLinks) {
  34.         this.entityLinks = checkNotNull(entityLinks);
  35.     }
  36.  
  37.     @GetMapping
  38.     public HttpEntity<Resources<Advertisement>> list() {
  39.         List<Advertisement> advertisements = Arrays.asList(
  40.                 new Advertisement("id-111", "Advertisement 1"),
  41.                 new Advertisement("id-222", "Advertisement 2"),
  42.                 new Advertisement("id-333", "Advertisement 3")
  43.         );
  44.         Resources<Advertisement> resources = new Resources<>(advertisements);
  45.         resources.add(entityLinks.linkToCollectionResource(Advertisement.class));
  46.  
  47.         return new ResponseEntity<>(resources, HttpStatus.OK);
  48.     }
  49.  
  50. }
Add Comment
Please, Sign In to add comment