A walk through building a micro service using Spring Boot.
Deck presented at Java 2016
Source accompanying presentation can be found at https://github.com/ospector/sbdemo
2. omri.spector@develeap.com
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
3. omri.spector@develeap.com
Agenda
• What is Spring Boot
• Creating a SpringBoot Application
• Creating a basic REST Service (POST, GET)
• Connecting to a Database
• Going forward
4. omri.spector@develeap.com
What is Spring boot
• Spring
• Opinionated
• Geared for fast development of robust micro
services
• Others in this arena
– Drop wizard
– Play
– Vert.x
– Jodd
– …
5. omri.spector@develeap.com
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”
7. omri.spector@develeap.com
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”,…
8. omri.spector@develeap.com
Alternative start - JHipster
• JHipster is a code generation tool that
offers a much more comprehensive
starting 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
9. omri.spector@develeap.com
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
11. omri.spector@develeap.com
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
13. omri.spector@develeap.com
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
14. omri.spector@develeap.com
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);
15. omri.spector@develeap.com
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
16. omri.spector@develeap.com
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…
17. omri.spector@develeap.com
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