Sie sind auf Seite 1von 5

Tutorial Spring Boot Quick Tutorial - udemy

Web app from scratch (Social Media Type platform)


- Goal: From being a fledgling developer to an experienced one
- Focusing in Customer Experience and forget all about backend, infrastructure and
configuration

Spring Boot App


Spring Initializr

Start Web app from scratch


then
Add features to it
***********
1. - start.spring.io
- Choose persistence, security, web and more (dependencies for project)
- Integrate with IDE

2.
=> start.spring.io and select dependencies for a project
WebSite navigator with:
Spring Initializr
Base of Spring Boot App
=> Select Gradele, version 2.0.3 (stable version on July 2018)
=> Artifacts for project, group name: pick a domain you have control over
=> Dependencies, Spring: (Web)MVC, (DB)Mongo, Security (if not, just take a tour of
it)
Clic "Switch to the full version" to list all dependencies (list a detail view)
Packaging file (jar, war) "make jar not war"

=> And click "Generate Project"

3. Picking data persistence solution


(start.spring.io)
Select web template engine
adding product-grade features
fitting the project with security

=> Every app that it bill, HAS TO STORE DATA.


JPA fit the bill and
(JPA is not going to be the best solution, but is the one used in the course)
H2 as embedded support
Thymeleaf as Template engine HTML + JSP
(Spring initializr, covers all dependencies of selected feature)

=> DevTools = Scala (changes on the fly)


(after all items are select, just click on "Generat Project")

=> Generate Project


(to download the code generated)

4. Download a generated project


=>
- start.spring.io
- metadata (as wished)
com.greglturnquist.learnspringboot (group)
learning-spring-boot-video (artifact)
- dependencies
JPA, H2, Thymeleaf, Actuator, DevTools, RemoteShell
- download it to local system
- import to Eclipse as Gradle project
- looking at the code:
* build.gradle: buildscript version, dependencies and project's definitions
(better than Maven)
(the version of dependencies, depend on the spring-boot version to fit it
better)
* tiny class with spring icon, @SpringBootApplication annotation, and main
method to call the starting class
* test folder to mockup
* resources for you webapp, static, templates for thymeleaf, properties
=> Run it with main class

***************************
***************************
***************************
***************************
***************************
***************************
***************************
***************************

Tutorial Spring Boot Microservices - Udemy

1. Download and Configure IDE


Eclipse popular environment for creating Java applications
IntelliJ IDEA nicer interface
Spring Tool Suite (STS), best IDE for developing Spring Boot applications
- Just Download: from spring.io/tools
- Extract the file

2.
Download Spring Tool Suite
Start
Create a new Workspace
Create New Maven Project
To add different things to a project
Select "a Simple Project"
To do not add "crazy amount of things to a project"
Group Id used to be the company's name

3. Add Spring Framework to the project


Guides:
https://spring.io/guides/gs/spring-boot/
https://spring.io/guides/gs/spring-boot/

- copy parent and dependencies


- adds maven dependencies,
***** PARENT org.springframework.boot Version 2.0.3
We do not have to care of dependencies
- the JDK should be 1.8
- Project -> Right click -> Maven -> Update Project in case of warnings in the
project

4. Set the starting point


- Create a class with public static void main (String... args) methodd
- @SpringBootApplication annotation in the class and import it
- in main method, SpringApplication.run(GroceryApiApplication.class,args);

5. Create a Controller section


- @RestController - class
- @RequestMapping ("....") - methods
- @RequestMapping(method = RequestMethod.POST, value = "/shop")

Example:
@RequestMapping(method = RequestMethod.DELETE, value="/shop")
public void deleteShop(@RequestBody ShoppingDTO shop) {
this.service.deleteShop(shop.getId());
}
@RequestMapping(method = RequestMethod.POST, value = "/shop")
public void addShopping(@RequestBody ShoppingDTO shop) {
this.service.addShop(shop);
}
@RequestMapping("/shop/{id}")
public ShoppingDTO getShoppingById(@PathVariable int id) {
return this.service.getShoppingById(id);
}

6. Using JSON Objects


- Create a model class
- Add properties get/set
We dont care about JSON format, Spring Boot automatically converts it to JSON
with Microservices

7. Service
From where the data is collected
@Service to import in the project and create a private property in the controller

this.listShopping.stream().filter(x -> x.getId() == id).findFirst().get();


[collection] [filter element] (TypeOf Element Lambda operation). get()
Array.asList () => Inmutable, you cannt add or remove items

********
ANNOTATIONS

@SpringBootApplication
@RestController - class
@RequestMapping ("....") - methods, GET
@RequestMapping(method = RequestMethod.POST, value = "/shop")
method(@RequestBody ShoppingDTO shop)
method(@PathVariable int id) - annotated by @RequestMapping("/shop/{id}")
@Service - class
@Autowired into class in the class property,
to use the DependencyInjection of Spring class, annotated by @Service

********
JPA (Java Persistence API) provides: ORM
ORM(Object Relational Mapping), to store objects in DB (relationalDB)
DBs used to be: Relational DBA
ORM, convert your classes representing by tables in DB

-----SPRING DATA JPA-----


Provides assistance to the developer to work with DB

Steps (FAIL, WORKS WITH 2.0.4 version)


- New Spring Starter Project
- Name it
- Select Dependencies
(same version as base project, now is 2.0.3)
* Web: Web to create SpringBoot dependency to the project
* SQL: JPA, Apache Derby
- Next
- Finish

Steps (WORK)
Create New Maven Project
To add different things to a project
Select "a Simple Project"
To do not add "crazy amount of things to a project"
Group Id used to be the company's name
Add Spring Framework to the project
Guides: https://spring.io/guides/gs/spring-boot/
- copy parent and dependencies
- adds maven dependencies,
***** PARENT org.springframework.boot Version 2.0.3
We do not have to care of dependencies
- the JDK should be 1.8
- Project -> Right click -> Maven -> Update Project in case of warnings in
the project

1. JPA
- After Squeleton
+ Create new class to communicate with DB
<Model>Repository
- Annote as @Entity on the Model, to store in DB
Same name as Table
- Annote as @Id the PK
Same name as PK and all properties
- The interface extending CrudRepository (interface from SpringBoot)
- Define objects of interface CrudRepository<DTO,PK>

2. get all DTO


- The service will take care of the Repository
- @Autowire the property of the Repositoy
- from method to get all DTO, use the method 'findAll()'
@Autowired
private ShoppingRepository repository;
...
public List<ShoppingDTO> getShoppingList(){
List<ShoppingDTO> listShoppingDTO = new ArrayList<ShoppingDTO>();
this.repository.findAll().
forEach(listShoppingDTO::add);
// forEach(x -> listShoppingDTO.add(x));
return listShoppingDTO;
}
3. add DTO
- use the service and the repository by:
this.repository.save(<Object of TypeDTO>);

4. update DTO
- PUT URL method
- get from id by this.repository.findById(id).get()

5.

Das könnte Ihnen auch gefallen