building a rest service in minutes with spring boot

19

Click here to load reader

Upload: omri-spector

Post on 16-Apr-2017

459 views

Category:

Technology


10 download

TRANSCRIPT

Page 1: Building a REST Service in minutes with Spring Boot

Spring Boot

Building a REST Service in minutes

Page 2: Building a REST Service in minutes with Spring Boot

[email protected]

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

[email protected]

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

[email protected]

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

[email protected]

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

Let’s code

Page 7: Building a REST Service in minutes with Spring Boot

[email protected]

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

[email protected]

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

[email protected]

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

[email protected]

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

[email protected]

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 12: Building a REST Service in minutes with Spring Boot

[email protected]

Chapter 2 – HTTP Errors

• Return 404 using RuntimeException with @ResponseStatus

Page 13: Building a REST Service in minutes with Spring Boot

[email protected]

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

[email protected]

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

[email protected]

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

[email protected]

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

[email protected]

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

[email protected]

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

Thank you!

www.develeap.com

Slide Share:

http://www.slideshare.net/omrispector/building-a-rest-service-in-minutes-with-spring-boot