0% found this document useful (0 votes)
16 views7 pages

B 9

Spring
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
16 views7 pages

B 9

Spring
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 7
oartt2023 19:38, Intwoduction to Spring Data REST | Bacldung “ Introduction to Spring Data REST Essai httos //adsfreestarcom/? \Written by. baeldung (https //wwwlbaeldung.com/author/oae\dung) REST (https:// www baoldung.com/category/rest) Spring Data (https:// www.baeldung.com/category/persistence/spring-persistence/spring-data) roforonce Spring Data REST thttps://www:baoldung.com/tag/spring-data-rest) Get started with Spring Data JPA through the reference Learn Spring Data JPA course: >» CHECK OUT THE COURSE (/learn-spring-data-jpa-course) Explore the future of 3D Petar} ore tata ss ey hitpsslww-baeldung.com/spring-data-estintro we oart2023 19:38, Introduction to Spring Data REST | Baeldung a 5 (https /ads freestarcom/? utm_campaign-branding&iutm .medium-banner&utm_source-baeldung.com&utm. content-baeldung.incontent 2 1, Overview This article wil explain the basics of Spring Data REST (https //spring io/projects/spring-data-rest) and show how to use it to build a simple REST APL In general, Spring Data REST is built on top of the Spring Data project and makes it easy to build hypermedia~ driven REST web services that connect to Spring Data repositories - all using HAL as the driving hypermedia type. It takes away a lot of the manual work usually associated with such tasks and makes implementing basic CRUD functionality for web applications quite simple. 2. Maven Dependencies The following Maven dependencies are required for our simple application: oO “org.sprinefrarework.bost spring-boot-starter org.springfrarcwork.bootspring-boot-start rest org.springtrarework.boot spring-boot- start spac/artifectTa> depandercy> “con.h2datsbase cartifectIdn2 We decided to use Spring Boot for this example, but classic Spring will also work fine, We also chose to use the H2 embedded database in order to avoid any extra setup, but the example can be applied to any database, (https /ads freestarcom/? 3. Writing the Application ‘We will start by writing a domain object to represent a user of our website: hitpssvww-baeldung.com/spring-data-estintro an oartt2023 19:38, Introduction to Spring Data REST | Baeltung ay a Fait cass waster ( oO private Leng id; private String names Private string ena‘; 11 standard getters and setters Every user has a name and an email. as well as an automatically-generated id, Now we can write a simple repository eReposstoryRestRescurce(cotlectionfesourceRel = "users", ath = “userst) oO public ‘interface UserRenos‘tary extends PasingAneSortingRepositary { Listewebsizeuser> FindyWame(eParan("nane") String ane); ‘This is an interface that allows you to perform various operations with WebsiteUser objects. We also defined a custom query that wil provide a lst of users based on a given name. ‘The @RepositoryRestResource annotation is optional and is used to customize the REST endpoint. we decided to ‘omit it, Spring would automatically create an endpoint at" AwebsiteUsers’ instead of */users. Finally. we will write a standard Spring Boot main class to initialize the application: > fs ete enn intos//adsteestarcom/? pubic ease Sprites cater ¢ i public static vote mein(Stringl) args) ( Springapplicatton.run(Springbacakestapplication.class, args); } Thats it! We now have a fully-functional REST API Let's take a look at tin action 4. Accessing the REST API If we run the application and go to hitp://Localhost 8080/ (http: //Localhost 8080/) in a browser, we will receive the following JSON htipsitwnww.bacldung.comlspring-data-restinto an oartt2023 19:38, Introduction to Spring Data REST | Baeltung ks: “ oO users”: { ef": shtp://localhost:8080/users{?page,stze,sert}", "templated? + true h "profite” + ( Inree™ + "https //ecalhost 800 /profi le” > y y Asyou can see, there isa */users’ endpoint available, and it already has the “2oage’."?size" and *?sorf options. ‘There is also a standard */profie’ endpoint, which provides application metadata. tis important to note that the response is structured in a way that follows the constraints of the REST architecture style, Specifically il provides a Uniform interface and self-descriptive messages, This means that each message contains enough information to describe how to process the message. There are no users in our application yet, so going to http //tocathost 8080/users thttp//localhost 8080/users) ‘would just show an emply list of users. Lets use curl to add a user crane" + Neest, ‘eestdtest con", « ‘ ret : "http: //locathost:8688/users/2" » Mhebsiteuser" = ( ef" + "http: //lecalhest:080/users/2" ? 3 3 Lets take a look at the response headers as well ‘You willnotice that the returned content types “application /halyisort: HAL {http /stateless.co/hal_specificationhtm) is a simple format that gives a consistent and easy way to hyperlink between resources in your APL. The header also automatically contains the Location header, which is the address ‘we can use to access the newly created user We can now access this user at http://Localhost 8080 /users/1(http:/localhost 8080/users/1) htipsitwnww.bacldung.comlspring-data-restinto an oartt2023 19:38, Introduction to Spring Data REST | Baeltung “ oO name" + Neest, sonait" + "testdtest.con", oMeket 2 ‘ + Yhttp://Locathost:8080/users/2" 1 Ynttp://lecathest:6080/users/2" ‘You can also use curl or any other REST client to issue PUT, PATCH, and DELETE requests. It also is important to note that Spring Data REST automatically follows the principles of HATEOAS. HATEOAS {https:/spring.io/projects/spring-hateoas) is one of the constraints of the REST architecture style, and it means that hypertext should be used to find your way through the API Finally. lets try to access the custom query that we wrate earlier and find all users with the name ‘test’. This is done by going to http://Locathost 8080 /users/search/findByName?name-test {http //localhost8080/users/search/fndByName?name-test) © ct 0 ) v cs wutedar 2 salt: ret": *http://locathost:8089/users/search/ Findbysane?nane=test” 5. Conclusion ‘This tutorial demonstrated the basics of creating a simple REST API with Spring Data REST. The example used in this article can be found in the linked GitHub project {https./githulscom/eugenp/tutorials/lree/master/persistence-modules/spring-data-restl Get started with Spring 5 and Spring Boot 2, through the Learn Spring course: >> CHECK OUT THE COURSE (/1s-course-ond) htipsitwnww.bacldung.comlspring-data-restinto sr oart2023 19:38 Introduction to Spring Data REST | Baelung Learning to build your API with Spring? Download the E-book (/rest-api-spring-guide) Comments are closed on this article! ‘couRSES SERIES JACKSON JSON TUTORIAL /3HCxSON) SPRING PERSISTENCE TUTORIAL V/PERSISTENCS.A/THSPAING-SERIES) ‘ABOUT htipsitwnww.bacldung.comlspring-data-restinto oavta023 1038 Taerusacci vrata anel? htipsitwnww.bacldung.comlspring-data-restinto Introduction to Spring Data REST | Baeltung W

You might also like