Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Hypermedia-driven Web Services with Spring Data REST

977 views

Published on

Overview of Spring Data REST that is a part of the umbrella Spring Data project and makes it easy to build hypermedia-driven REST web services on top of Spring Data repositories. This presentation will be useful for software engineers who worked or are going to work with Spring Data and want to try out Spring Data REST for exposing a discoverable REST API for your domain model using HAL as a media type.

Published in: Technology
  • Be the first to comment

Hypermedia-driven Web Services with Spring Data REST

  1. 1. Hypermedia-driven Web Services with Spring Data REST 1
  2. 2. Sofiia Vynnytska 2
  3. 3. Agenda • HATEOAS • Spring Data REST • Demos 3
  4. 4. 44 supershirtguy.com/Funny-T-Shirts/Old%20School%20Homer%20Shirt%20Grey.php
  5. 5. Classic way Controller Service Repository Database 5
  6. 6. Simple Junior Engineers API • Create • Update • Delete • Get by Id • Get all 6 https://www.uvu.edu
  7. 7. DEMO TIME! 7
  8. 8. Pros and Cons + We know how it works + Everything is clear + Easy to debug - A lot of code for simple API - Code duplication - No business logic 8 stockfresh.com
  9. 9. Client Server Communication 9
  10. 10. says they want something called service. 10 Someone calling themselves a client www.newslettercartoons.com
  11. 11. 11 [{ "id": "1", "firstName": "Evgeny", "lastName": "Borisov" }, { "id": "2", "firstName": "Mikalai", "lastName ": "Alimenkou" }] "/juniors"
  12. 12. 12 "/juniors" + "/1" "/engineers/1" + "/skill?newSkill=" + "Java 9" { "id": "1", "firstName": "Evgeny", "lastName": "Borisov", "skills": ["Spring", "Spring Boot"] }
  13. 13. 13 memegenerator.net
  14. 14. 14 www.business2community.com
  15. 15. HATEOAS 15
  16. 16. WELL GIRL… WAIT WHAT DID YOU JUST SAY?16 http://www.express.co.uk/
  17. 17. I HATE OARS 17
  18. 18. 18 Hypermedia As The Engine Of Application State
  19. 19. 19 { "id": "1", "fullName": "Evgeny Borisov", "skills": ["Spring", "Spring Boot"] "_links": { "self": { "href":"http://localhost:8080/juniors/1" }, "add-java-9": { "href":"http://localhost:8080/juniors/1 /skill?newSkill=Java 9" } } }
  20. 20. Pros and Cons + Discoverability + It’s all about server + No client knowledge - Complex - Learning curve stockfresh.com 20
  21. 21. 21 http://www.thesimpleway.org/
  22. 22. Spring Data REST 22
  23. 23. It’s REST time 23
  24. 24. Spring Data REST Spring Data REST API Implementation Spring Data REST 24
  25. 25. Support DATA 25
  26. 26. Spring Data REST @RepositoryRestResource public interface JuniorEngineerRepository extends CrudRepository<JuniorEngineer, String> { } 26
  27. 27. Repository detection 27 biwizard.wordpress.com/
  28. 28. Repository detection strategy • Default • Annotation 28 • Visibility • All
  29. 29. Repository detection strategy spring.data.rest.detection-strategy=annotated 29
  30. 30. Hiding repository and methods @RepositoryRestResource(exported = false) public interface JuniorEngineerRepository extends CrudRepository<JuniorEngineer, String>{ @RestResource(exported = false) void delete(String id); } 30
  31. 31. Resources 31
  32. 32. Resource path @RepositoryRestResource public interface JuniorEngineerRepository extends CrudRepository<JuniorEngineer, String>{ } 32 "/juniorEngineers" (path = "juniors")
  33. 33. Collection resource 33
  34. 34. Collection resource GET 34 "/juniors" POST findAll() save(…)
  35. 35. Item resource 35
  36. 36. Item resource 36 "/juniors/{id}" GET PUT PATCH DELETE findOne(…) save (…) save (…) delete(…)
  37. 37. Search resource 37
  38. 38. 38 May I have a cigarette or should I search for it?
  39. 39. Search resource 39 @RepositoryRestResource(path = "juniors") public interface JuniorEngineerRepository extends CrudRepository<JuniorEngineer, String>{ @RestResource List<JuniorEngineer> findByProjectIsNull(); }
  40. 40. Search resource 40 "/juniors/search" "/juniors/search/findByProjectIsNull"
  41. 41. 41lh5.googleusercontent.com/-rcOOyxZszOw/VIdTCBTMHeI/AAAAAAAACp4/Z9QR4R1C89M/template-minion2.gif
  42. 42. Client specific representations 42
  43. 43. 43 www.elitereaders.com
  44. 44. Projections 44
  45. 45. Projections @Projection(types = JuniorEngineer.class, name = "minimal") public interface MinimalProjection { Set<String> getSkills(); @Value("#{target.firstName} #{target.lastName}") String getFullName(); } 45
  46. 46. Projections discovery • Same package as domain • Register manually RepositoryRestConfiguration .getProjectionConfiguration() .addProjection(…) 46
  47. 47. Excerpts 47
  48. 48. 48
  49. 49. Excerpts @RepositoryRestResource(excerptProjection = DefaultProjection.class) • Collection • No item 49
  50. 50. DEMO TIME! 50https://movies.alphacoders.com/movies/view/28/The-Matrix
  51. 51. Validation 51 ratiocinativa.files.wordpress.com
  52. 52. Validation ● Bind it by name @Component("beforeCreateSeniorValidator") public class SeniorValidator implements Validator {…} ● Register manually ValidatingRepositoryEventListener .addValidator("beforeCreate", new SeniorValidator()); 52
  53. 53. DEMO 53 TIME giphy.gif
  54. 54. Links 54
  55. 55. Links • new Link("localhost:8080/juniors", "juniors"); • ControllerLinkBuilder linkTo(), methodOn() • RepositoryEntityLinks 55
  56. 56. DEMO TIME! 56
  57. 57. Summary 57
  58. 58. Pros and Cons + Helps to start quickly + CRUD + Projections + Hypermedia - Debugging - Bugs - No Bean Validation(JSR-303) 58stockfresh.com
  59. 59. When not use Spring Data REST • Spring, Spring Data in your project • To much business logic 59
  60. 60. Summary • REST API • Projections • Validation • Links 60
  61. 61. Useful links Spring Data REST Docs Understanding HATEOAS Projection representation of single resource Custom methods validation 61
  62. 62. https://github.com/VynnytskaSofiia/demo- spring-data-rest 62 https://www.facebook.com/sofiia.vynnytska
  63. 63. 63cdn2.content.compendiumblog.com
  64. 64. 64 ww.pngall.com

×