SlideShare une entreprise Scribd logo
1  sur  36
Télécharger pour lire hors ligne
Introduction to Spring Boot
Why Spring Boot?
✦It’s popular & modern
✦It’s easy to use - practically like building a
command line app
✦Trivial to install
✦Provides integration to web services & Database
✦It’s still Java
✦It has integration to all major IDE’s
IDE Support
✦NetBeans - http://plugins.netbeans.org/plugin/
67888/nb-springboot
✦IntelliJ - https://www.jetbrains.com/help/idea/
2016.3/creating-spring-boot-projects.html
✦Eclipse - https://spring.io/tools/sts/all
So what is it?
Spring Boot makes it easy to create stand-alone,
production-grade Spring based Applications that
you can "just run". We take an opinionated view of
the Spring platform and third-party libraries so you
can get started with minimum fuss. Most Spring
Boot applications need very little Spring
configuration.
“
So what is it?
✦A set of tools that generate a single jar web app
✦Tomcat is bundled right in your fat jar
✦Literally just write a simple single class code
✦Deploy one jar to your server
MySQL
✦We will go with MySQL for storage
✦It’s easy to map it to JPA objects in Spring Boot
✦I chose it mostly for convenience as it had better
installers than MariaDB for Mac
✦You can use MariaDB as it should be compatible
✦MySQL is familiar, simple, performant & powerful
Why not NoSQL?
✦NoSQL is easier and more scalable on some
fronts but harder on others
✦Data migration to/from NoSQL is really difficult
✦Reporting and query 3rd party tools aren’t as easy
✦Moving to NoSQL is easier than moving out of
NoSQL architecture
✦E.g. when Google needed performance for
search they used NoSQL when they needed
reliability for adsense they used SQL
Mini
Mission
© Codename One 2017 all rights reserved
Setup MySQL
✦Setup and configure MySQL create a database
✦Once installed launch the database daemon
process
✦Login to MySQL using a command similar to this:
/usr/local/mysql/bin/mysql -h localhost -u root -p
Configure Database
✦We’ll do the initial work in the test database
✦To create it we will need to issue:
CREATE DATABASE test;
✦Notice the db might require you to set your
password the first time around
UPDATE mysql.user SET
Password=PASSWORD('your_new_password') WHERE User='root';
Spring Boot
© Codename One 2017 all rights reserved
Spring Boot
✦Once the IDE plugin is installed getting started
with Spring Boot is really easy
✦In netbeans it looks roughly like this…
New Project
New Project
Initializr Project
Notice the specific project
type selection. This will allow
us to pick features for
inclusion later
New Project
New Project
Names
Most of these don’t matter
much for the test app except
for the package name & app
name which you will need to
use afterwards
New Project
New Project
Selections
Make sure to pick the
following: Web, Websocket,
Jersey, JPA & MySQL.
There are many other
interesting options but I want
to keep things simple
New Project
New Project
Important
After creation you might need
to resolve project issues so
maven can fetch
dependencies
Configure Database
✦Open application.properties
✦Configure mysql:
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=root
spring.datasource.password=*****
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
The Main App
✦The main app of bootstrap doesn’t do much:
package com.codename1.testapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class TestAppApplication {
public static void main(String[] args) {
SpringApplication.run(TestAppApplication.class, args);
}
}
Something
Useful
© Codename One 2017 all rights reserved
JPA Storage
✦Lets store some elements into MySQL
✦We’ll use JPA (Hibernate)
✦JPA allows us to map objects to database tables
and mostly hides SQL
✦I won’t go into the working/philosophy behind it
and use it in a relatively simplistic way
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Item {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
private boolean marked;
private boolean deleted;
private String owner;
public Item() {}
public Item(Long id, String name, boolean marked, boolean deleted, String owner) {
…
}
}
Item
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Item {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String name;
private boolean marked;
private boolean deleted;
private String owner;
public Item() {}
public Item(Long id, String name, boolean marked, boolean deleted, String owner) {
…
}
}
Item
Trimmed Code
I trimmed a lot of boilerplate
code, the method body,
getters/setters to all the
elements
package com.codename1.testapp;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface ItemRepository extends CrudRepository<Item, Long> {
List<Item> findByOwner(String owner);
}
ItemRepository
package com.codename1.testapp;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface ItemRepository extends CrudRepository<Item, Long> {
List<Item> findByOwner(String owner);
}
ItemRepository
CRUD
Spring injects the
implementation of this interface.
It includes API’s for Create,
Read, Update & Delete for the
entity.
@Controller
@RequestMapping("/item")
public class ItemService {
@Autowired
private ItemRepository repo;
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody Item[] go(@RequestParam(value="id", required=false) Long id,
@RequestParam(value="owner", required=false) String owner) {
if(id != null) {
Item c = repo.findOne(id);
return new Item[] {c};
}
if(owner != null) {
List<Item> c = repo.findByOwner(owner);
Item[] i = new Item[c.size()];
return c.toArray(i);
}
return new Item[0];
}
@RequestMapping(method=RequestMethod.PUT)
public @ResponseBody Item add(@RequestBody(required=true) Item i) {
i = repo.save(i);
return i;
}
}
ItemService
@Controller
@RequestMapping("/item")
public class ItemService {
@Autowired
private ItemRepository repo;
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody Item[] go(@RequestParam(value="id", required=false) Long id,
@RequestParam(value="owner", required=false) String owner) {
if(id != null) {
Item c = repo.findOne(id);
return new Item[] {c};
}
if(owner != null) {
List<Item> c = repo.findByOwner(owner);
Item[] i = new Item[c.size()];
return c.toArray(i);
}
return new Item[0];
}
@RequestMapping(method=RequestMethod.PUT)
public @ResponseBody Item add(@RequestBody(required=true) Item i) {
i = repo.save(i);
return i;
}
}
ItemService
Injection
This value is injected implicitly
by Spring
@Controller
@RequestMapping("/item")
public class ItemService {
@Autowired
private ItemRepository repo;
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody Item[] go(@RequestParam(value="id", required=false) Long id,
@RequestParam(value="owner", required=false) String owner) {
if(id != null) {
Item c = repo.findOne(id);
return new Item[] {c};
}
if(owner != null) {
List<Item> c = repo.findByOwner(owner);
Item[] i = new Item[c.size()];
return c.toArray(i);
}
return new Item[0];
}
@RequestMapping(method=RequestMethod.PUT)
public @ResponseBody Item add(@RequestBody(required=true) Item i) {
i = repo.save(i);
return i;
}
}
ItemService
@Controller
@RequestMapping("/item")
public class ItemService {
@Autowired
private ItemRepository repo;
@RequestMapping(method=RequestMethod.GET)
public @ResponseBody Item[] go(@RequestParam(value="id", required=false) Long id,
@RequestParam(value="owner", required=false) String owner) {
if(id != null) {
Item c = repo.findOne(id);
return new Item[] {c};
}
if(owner != null) {
List<Item> c = repo.findByOwner(owner);
Item[] i = new Item[c.size()];
return c.toArray(i);
}
return new Item[0];
}
@RequestMapping(method=RequestMethod.PUT)
public @ResponseBody Item add(@RequestBody(required=true) Item i) {
i = repo.save(i);
return i;
}
}
ItemService
Bad Design
I’m returning the Item object
which is an Entity & an internal
representation of the database.
This is bad design as I might
expose internal state! 

I’m doing it here as a shortcut
Add an Item
✦We can add an item using this curl request:
curl -H "Content-Type: application/json" -X PUT 
-d '{"name":"Item Name", "marked":false, "deleted":false, "owner":"Shai"}' 
http://localhost:8080/item
✦The output is:
{"id":1,"name":"Item Name","marked":false,"deleted":false,"owner":"Shai"}
Fetch My Items
✦We can fetch my items using:
curl http://localhost:8080/item?owner=Shai
✦The output is:
[{"id":1,"name":"Item Name","marked":false,"deleted":false,"owner":"Shai"},
{"id":2,"name":"Item Name","marked":false,"deleted":false,"owner":"Shai"},
{"id":3,"name":"Item Name","marked":false,"deleted":false,"owner":"Shai"}]
What did we learn?
✦Why we chose Spring Boot and why it’s cool
✦How to set it up with MySQL
✦Implementing a restful service that’s moderately
useful with almost no code…
Thank You

Contenu connexe

Similaire à Introduction to Spring Boot.pdf

Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolGordon Forsythe
 
Java EE and Spring Side-by-Side
Java EE and Spring Side-by-SideJava EE and Spring Side-by-Side
Java EE and Spring Side-by-SideReza Rahman
 
Parse cloud code
Parse cloud codeParse cloud code
Parse cloud code維佋 唐
 
Salesforce meetup | Lightning Web Component
Salesforce meetup | Lightning Web ComponentSalesforce meetup | Lightning Web Component
Salesforce meetup | Lightning Web ComponentAccenture Hungary
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to javaciklum_ods
 
Introduction To Code Igniter
Introduction To Code IgniterIntroduction To Code Igniter
Introduction To Code IgniterAmzad Hossain
 
Developer Student Clubs NUK - Flutter for Beginners
Developer Student Clubs NUK - Flutter for BeginnersDeveloper Student Clubs NUK - Flutter for Beginners
Developer Student Clubs NUK - Flutter for BeginnersJiaxuan Lin
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedGil Fink
 
From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)Jose Manuel Pereira Garcia
 
AI: Mobile Apps That Understands Your Intention When You Typed
AI: Mobile Apps That Understands Your Intention When You TypedAI: Mobile Apps That Understands Your Intention When You Typed
AI: Mobile Apps That Understands Your Intention When You TypedMarvin Heng
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android InfrastructureAlexey Buzdin
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android InfrastructureC.T.Co
 
React Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsReact Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsMatteo Manchi
 
Mobile App Development: Primi passi con NativeScript e Angular 2
Mobile App Development: Primi passi con NativeScript e Angular 2Mobile App Development: Primi passi con NativeScript e Angular 2
Mobile App Development: Primi passi con NativeScript e Angular 2Filippo Matteo Riggio
 
Enterprise Guice 20090217 Bejug
Enterprise Guice 20090217 BejugEnterprise Guice 20090217 Bejug
Enterprise Guice 20090217 Bejugrobbiev
 
ASP.Net 5 and C# 6
ASP.Net 5 and C# 6ASP.Net 5 and C# 6
ASP.Net 5 and C# 6Andy Butland
 

Similaire à Introduction to Spring Boot.pdf (20)

Real World MVC
Real World MVCReal World MVC
Real World MVC
 
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
 
Having Fun with Play
Having Fun with PlayHaving Fun with Play
Having Fun with Play
 
Java EE and Spring Side-by-Side
Java EE and Spring Side-by-SideJava EE and Spring Side-by-Side
Java EE and Spring Side-by-Side
 
Parse cloud code
Parse cloud codeParse cloud code
Parse cloud code
 
Grails Advanced
Grails Advanced Grails Advanced
Grails Advanced
 
Salesforce meetup | Lightning Web Component
Salesforce meetup | Lightning Web ComponentSalesforce meetup | Lightning Web Component
Salesforce meetup | Lightning Web Component
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Gwt.create
Gwt.createGwt.create
Gwt.create
 
Introduction To Code Igniter
Introduction To Code IgniterIntroduction To Code Igniter
Introduction To Code Igniter
 
Developer Student Clubs NUK - Flutter for Beginners
Developer Student Clubs NUK - Flutter for BeginnersDeveloper Student Clubs NUK - Flutter for Beginners
Developer Student Clubs NUK - Flutter for Beginners
 
Stencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrivedStencil the time for vanilla web components has arrived
Stencil the time for vanilla web components has arrived
 
From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)From Legacy to Hexagonal (An Unexpected Android Journey)
From Legacy to Hexagonal (An Unexpected Android Journey)
 
AI: Mobile Apps That Understands Your Intention When You Typed
AI: Mobile Apps That Understands Your Intention When You TypedAI: Mobile Apps That Understands Your Intention When You Typed
AI: Mobile Apps That Understands Your Intention When You Typed
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
React Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsReact Native for multi-platform mobile applications
React Native for multi-platform mobile applications
 
Mobile App Development: Primi passi con NativeScript e Angular 2
Mobile App Development: Primi passi con NativeScript e Angular 2Mobile App Development: Primi passi con NativeScript e Angular 2
Mobile App Development: Primi passi con NativeScript e Angular 2
 
Enterprise Guice 20090217 Bejug
Enterprise Guice 20090217 BejugEnterprise Guice 20090217 Bejug
Enterprise Guice 20090217 Bejug
 
ASP.Net 5 and C# 6
ASP.Net 5 and C# 6ASP.Net 5 and C# 6
ASP.Net 5 and C# 6
 

Plus de ShaiAlmog1

The Duck Teaches Learn to debug from the masters. Local to production- kill ...
The Duck Teaches  Learn to debug from the masters. Local to production- kill ...The Duck Teaches  Learn to debug from the masters. Local to production- kill ...
The Duck Teaches Learn to debug from the masters. Local to production- kill ...ShaiAlmog1
 
create-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfcreate-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfShaiAlmog1
 
create-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfcreate-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfShaiAlmog1
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfShaiAlmog1
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfShaiAlmog1
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfShaiAlmog1
 
create-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfcreate-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfShaiAlmog1
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfShaiAlmog1
 
create-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfcreate-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfShaiAlmog1
 
create-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfcreate-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfShaiAlmog1
 
create-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfcreate-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfShaiAlmog1
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfShaiAlmog1
 
create-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfcreate-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdfCreating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfCreating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfCreating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfCreating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfCreating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfCreating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfShaiAlmog1
 
Creating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfCreating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfShaiAlmog1
 

Plus de ShaiAlmog1 (20)

The Duck Teaches Learn to debug from the masters. Local to production- kill ...
The Duck Teaches  Learn to debug from the masters. Local to production- kill ...The Duck Teaches  Learn to debug from the masters. Local to production- kill ...
The Duck Teaches Learn to debug from the masters. Local to production- kill ...
 
create-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdfcreate-netflix-clone-06-client-ui.pdf
create-netflix-clone-06-client-ui.pdf
 
create-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdfcreate-netflix-clone-01-introduction_transcript.pdf
create-netflix-clone-01-introduction_transcript.pdf
 
create-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdfcreate-netflix-clone-02-server_transcript.pdf
create-netflix-clone-02-server_transcript.pdf
 
create-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdfcreate-netflix-clone-04-server-continued_transcript.pdf
create-netflix-clone-04-server-continued_transcript.pdf
 
create-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdfcreate-netflix-clone-01-introduction.pdf
create-netflix-clone-01-introduction.pdf
 
create-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdfcreate-netflix-clone-06-client-ui_transcript.pdf
create-netflix-clone-06-client-ui_transcript.pdf
 
create-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdfcreate-netflix-clone-03-server.pdf
create-netflix-clone-03-server.pdf
 
create-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdfcreate-netflix-clone-04-server-continued.pdf
create-netflix-clone-04-server-continued.pdf
 
create-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdfcreate-netflix-clone-05-client-model_transcript.pdf
create-netflix-clone-05-client-model_transcript.pdf
 
create-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdfcreate-netflix-clone-03-server_transcript.pdf
create-netflix-clone-03-server_transcript.pdf
 
create-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdfcreate-netflix-clone-02-server.pdf
create-netflix-clone-02-server.pdf
 
create-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdfcreate-netflix-clone-05-client-model.pdf
create-netflix-clone-05-client-model.pdf
 
Creating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdfCreating a Whatsapp Clone - Part II.pdf
Creating a Whatsapp Clone - Part II.pdf
 
Creating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdfCreating a Whatsapp Clone - Part IX - Transcript.pdf
Creating a Whatsapp Clone - Part IX - Transcript.pdf
 
Creating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdfCreating a Whatsapp Clone - Part II - Transcript.pdf
Creating a Whatsapp Clone - Part II - Transcript.pdf
 
Creating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdfCreating a Whatsapp Clone - Part V - Transcript.pdf
Creating a Whatsapp Clone - Part V - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdfCreating a Whatsapp Clone - Part IV - Transcript.pdf
Creating a Whatsapp Clone - Part IV - Transcript.pdf
 
Creating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdfCreating a Whatsapp Clone - Part IV.pdf
Creating a Whatsapp Clone - Part IV.pdf
 
Creating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdfCreating a Whatsapp Clone - Part I - Transcript.pdf
Creating a Whatsapp Clone - Part I - Transcript.pdf
 

Dernier

WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 

Dernier (20)

WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

Introduction to Spring Boot.pdf

  • 2. Why Spring Boot? ✦It’s popular & modern ✦It’s easy to use - practically like building a command line app ✦Trivial to install ✦Provides integration to web services & Database ✦It’s still Java ✦It has integration to all major IDE’s
  • 3. IDE Support ✦NetBeans - http://plugins.netbeans.org/plugin/ 67888/nb-springboot ✦IntelliJ - https://www.jetbrains.com/help/idea/ 2016.3/creating-spring-boot-projects.html ✦Eclipse - https://spring.io/tools/sts/all
  • 4. So what is it? Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration. “
  • 5. So what is it? ✦A set of tools that generate a single jar web app ✦Tomcat is bundled right in your fat jar ✦Literally just write a simple single class code ✦Deploy one jar to your server
  • 6. MySQL ✦We will go with MySQL for storage ✦It’s easy to map it to JPA objects in Spring Boot ✦I chose it mostly for convenience as it had better installers than MariaDB for Mac ✦You can use MariaDB as it should be compatible ✦MySQL is familiar, simple, performant & powerful
  • 7. Why not NoSQL? ✦NoSQL is easier and more scalable on some fronts but harder on others ✦Data migration to/from NoSQL is really difficult ✦Reporting and query 3rd party tools aren’t as easy ✦Moving to NoSQL is easier than moving out of NoSQL architecture ✦E.g. when Google needed performance for search they used NoSQL when they needed reliability for adsense they used SQL
  • 8. Mini Mission © Codename One 2017 all rights reserved
  • 9. Setup MySQL ✦Setup and configure MySQL create a database ✦Once installed launch the database daemon process ✦Login to MySQL using a command similar to this: /usr/local/mysql/bin/mysql -h localhost -u root -p
  • 10. Configure Database ✦We’ll do the initial work in the test database ✦To create it we will need to issue: CREATE DATABASE test; ✦Notice the db might require you to set your password the first time around UPDATE mysql.user SET Password=PASSWORD('your_new_password') WHERE User='root';
  • 11. Spring Boot © Codename One 2017 all rights reserved
  • 12. Spring Boot ✦Once the IDE plugin is installed getting started with Spring Boot is really easy ✦In netbeans it looks roughly like this…
  • 14. New Project Initializr Project Notice the specific project type selection. This will allow us to pick features for inclusion later
  • 16. New Project Names Most of these don’t matter much for the test app except for the package name & app name which you will need to use afterwards
  • 18. New Project Selections Make sure to pick the following: Web, Websocket, Jersey, JPA & MySQL. There are many other interesting options but I want to keep things simple
  • 20. New Project Important After creation you might need to resolve project issues so maven can fetch dependencies
  • 21. Configure Database ✦Open application.properties ✦Configure mysql: spring.datasource.url=jdbc:mysql://localhost/test spring.datasource.username=root spring.datasource.password=***** spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.hibernate.ddl-auto=update spring.jpa.generate-ddl=true spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
  • 22. The Main App ✦The main app of bootstrap doesn’t do much: package com.codename1.testapp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class TestAppApplication { public static void main(String[] args) { SpringApplication.run(TestAppApplication.class, args); } }
  • 23. Something Useful © Codename One 2017 all rights reserved
  • 24. JPA Storage ✦Lets store some elements into MySQL ✦We’ll use JPA (Hibernate) ✦JPA allows us to map objects to database tables and mostly hides SQL ✦I won’t go into the working/philosophy behind it and use it in a relatively simplistic way
  • 25. import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Item { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; private String name; private boolean marked; private boolean deleted; private String owner; public Item() {} public Item(Long id, String name, boolean marked, boolean deleted, String owner) { … } } Item
  • 26. import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Item { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; private String name; private boolean marked; private boolean deleted; private String owner; public Item() {} public Item(Long id, String name, boolean marked, boolean deleted, String owner) { … } } Item Trimmed Code I trimmed a lot of boilerplate code, the method body, getters/setters to all the elements
  • 27. package com.codename1.testapp; import java.util.List; import org.springframework.data.repository.CrudRepository; public interface ItemRepository extends CrudRepository<Item, Long> { List<Item> findByOwner(String owner); } ItemRepository
  • 28. package com.codename1.testapp; import java.util.List; import org.springframework.data.repository.CrudRepository; public interface ItemRepository extends CrudRepository<Item, Long> { List<Item> findByOwner(String owner); } ItemRepository CRUD Spring injects the implementation of this interface. It includes API’s for Create, Read, Update & Delete for the entity.
  • 29. @Controller @RequestMapping("/item") public class ItemService { @Autowired private ItemRepository repo; @RequestMapping(method=RequestMethod.GET) public @ResponseBody Item[] go(@RequestParam(value="id", required=false) Long id, @RequestParam(value="owner", required=false) String owner) { if(id != null) { Item c = repo.findOne(id); return new Item[] {c}; } if(owner != null) { List<Item> c = repo.findByOwner(owner); Item[] i = new Item[c.size()]; return c.toArray(i); } return new Item[0]; } @RequestMapping(method=RequestMethod.PUT) public @ResponseBody Item add(@RequestBody(required=true) Item i) { i = repo.save(i); return i; } } ItemService
  • 30. @Controller @RequestMapping("/item") public class ItemService { @Autowired private ItemRepository repo; @RequestMapping(method=RequestMethod.GET) public @ResponseBody Item[] go(@RequestParam(value="id", required=false) Long id, @RequestParam(value="owner", required=false) String owner) { if(id != null) { Item c = repo.findOne(id); return new Item[] {c}; } if(owner != null) { List<Item> c = repo.findByOwner(owner); Item[] i = new Item[c.size()]; return c.toArray(i); } return new Item[0]; } @RequestMapping(method=RequestMethod.PUT) public @ResponseBody Item add(@RequestBody(required=true) Item i) { i = repo.save(i); return i; } } ItemService Injection This value is injected implicitly by Spring
  • 31. @Controller @RequestMapping("/item") public class ItemService { @Autowired private ItemRepository repo; @RequestMapping(method=RequestMethod.GET) public @ResponseBody Item[] go(@RequestParam(value="id", required=false) Long id, @RequestParam(value="owner", required=false) String owner) { if(id != null) { Item c = repo.findOne(id); return new Item[] {c}; } if(owner != null) { List<Item> c = repo.findByOwner(owner); Item[] i = new Item[c.size()]; return c.toArray(i); } return new Item[0]; } @RequestMapping(method=RequestMethod.PUT) public @ResponseBody Item add(@RequestBody(required=true) Item i) { i = repo.save(i); return i; } } ItemService
  • 32. @Controller @RequestMapping("/item") public class ItemService { @Autowired private ItemRepository repo; @RequestMapping(method=RequestMethod.GET) public @ResponseBody Item[] go(@RequestParam(value="id", required=false) Long id, @RequestParam(value="owner", required=false) String owner) { if(id != null) { Item c = repo.findOne(id); return new Item[] {c}; } if(owner != null) { List<Item> c = repo.findByOwner(owner); Item[] i = new Item[c.size()]; return c.toArray(i); } return new Item[0]; } @RequestMapping(method=RequestMethod.PUT) public @ResponseBody Item add(@RequestBody(required=true) Item i) { i = repo.save(i); return i; } } ItemService Bad Design I’m returning the Item object which is an Entity & an internal representation of the database. This is bad design as I might expose internal state! 
 I’m doing it here as a shortcut
  • 33. Add an Item ✦We can add an item using this curl request: curl -H "Content-Type: application/json" -X PUT -d '{"name":"Item Name", "marked":false, "deleted":false, "owner":"Shai"}' http://localhost:8080/item ✦The output is: {"id":1,"name":"Item Name","marked":false,"deleted":false,"owner":"Shai"}
  • 34. Fetch My Items ✦We can fetch my items using: curl http://localhost:8080/item?owner=Shai ✦The output is: [{"id":1,"name":"Item Name","marked":false,"deleted":false,"owner":"Shai"}, {"id":2,"name":"Item Name","marked":false,"deleted":false,"owner":"Shai"}, {"id":3,"name":"Item Name","marked":false,"deleted":false,"owner":"Shai"}]
  • 35. What did we learn? ✦Why we chose Spring Boot and why it’s cool ✦How to set it up with MySQL ✦Implementing a restful service that’s moderately useful with almost no code…