building a rest service in minutes with spring boot
TRANSCRIPT
![Page 1: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/1.jpg)
Spring Boot
Building a REST Service in minutes
![Page 2: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/2.jpg)
I am…• Omri Spector, CEO deveLeap• deveLeap mission– Helping development teams use the right tools,
technologies & architecture to achieve their business objective.
– Specialize in Java, CI and the JVM eco-system• Omri– Active commercial developer since 1985– In Java since 2004– Managed large multi national development groups
![Page 3: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/3.jpg)
Agenda
• What is Spring Boot• Creating a SpringBoot Application• Creating a basic REST Service (POST, GET)• Connecting to a Database• Going forward
![Page 4: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/4.jpg)
What is Spring boot
• Spring• Opinionated• Geared for fast development of robust micro services• Others in this arena– Drop wizard– Play– Vert.x– Jodd– …
![Page 5: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/5.jpg)
Where would I use SpringBoot?
• Green Field – Get new project up & running in no time without compromising architecture and infrastructure
• Modernizing Legacy – Gradually modernize legacy monolith applications by moving aspects to Spring Boot “micro-services”
![Page 6: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/6.jpg)
Let’s code
![Page 7: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/7.jpg)
Starting a project
• https://start.spring.io/– Actuator: “Production Ready”– Web: Web related functionality (e.g. MVC)– Devtools: Hot swap during development– Lombok: “Less boilerplate”
Later we can easily add more, e.g. “data”, “cloud”,…
![Page 8: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/8.jpg)
Alternative start - JHipster• JHipster is a code generation tool that
offers a much more comprehensivestarting point
• It adds opinioned use of the client side stack:Angular, Gulp, SASS, Bower, etc.
• It adds many common features and UI out of the box• The amount of code it creates is large – which can be
viewed as good or bad
![Page 9: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/9.jpg)
What we created
• Pom – spring boot pom• DemoApplication – – @SpringBootApplication: spring mvc, scanner– Main: embedded tomcat
• Resources/static, resources/template• Application.properties (empty)• DemoApplicationTests – context loads, Spring
test
![Page 10: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/10.jpg)
Build & Run
• Fat Jar for easy deploy• Actuator endpoints:– /health, – /env, – /metrics,– :
• Hot swap
![Page 11: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/11.jpg)
Chapter 1 – POST, GET• Domain: Simplify with lombok• Services: @Service pojo for all business logic• Controller: @Controller utilizing:– @RequestMapping (hierarchical)– @ResponseBody – to avoid writing views– @RequestBody & @PathVariable on params
• PostMan – Post, Get, Get non existing
![Page 13: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/13.jpg)
Chapter 3 – Working with a DB• Add spring-boot-starter-data-jpa and a db driver• Add a CrudRepository based @Repository• Model object is now and @Entity with an @id– Make sure name matches table name!
• Changed service to use it• Configuring the db connection in application
properties
![Page 14: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/14.jpg)
Chapter 4 – Getting a list• Many options, from findAll to HQL to SQL• We will use Spring Data Proxy
• Of course this is naïve, and is often improved:– Limiting the fields returned or even defining a “light”
version– Adding sort and paging
Iterable<Artist> findByNameContaining(String s);
![Page 15: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/15.jpg)
Chapter 5 – It doesn’t stop here
• Spring is a very comprehensive project• Spring-Boot wraps many of it’s aspects and
integrates many other successful open source facilities
![Page 16: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/16.jpg)
Typical additions• Spring Security• Spring Social – Facebook, Twitter, LinkedIn…• Spring Cloud – Service discovery, Circuit breakers,
control bus, …• Schema Management – Liquibase, Flyway• Front end assets management – Web jars• Transaction Management• Spring Data – Both Relational and many NoSQL And the list goes on…
![Page 17: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/17.jpg)
In Summary• Spring boot creates an:– easy to deploy – executable – “fat” jar
• Management services out of the box• In several minutes and very little code we get:– REST against a db backend– JPA based ORM– And the spring power to easily add so much more
![Page 18: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/18.jpg)
Shameless Plug
Are you considering the move to “micro-services”?
Don’t hesitate to call: [email protected]
![Page 19: Building a REST Service in minutes with Spring Boot](https://reader038.vdocuments.us/reader038/viewer/2022100504/586fdbda1a28ab18428b62c5/html5/thumbnails/19.jpg)
Thank you!
www.develeap.com
Slide Share:
http://www.slideshare.net/omrispector/building-a-rest-service-in-minutes-with-spring-boot