Tutorial SpringBoot
Tutorial SpringBoot
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"
***************************
***************************
***************************
***************************
***************************
***************************
***************************
***************************
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
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);
}
7. Service
From where the data is collected
@Service to import in the project and create a private property in the controller
********
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
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>
4. update DTO
- PUT URL method
- get from id by this.repository.findById(id).get()
5.