Day5 - SpringBootApplication Annotation - Coding RestAPI Around Resources - 8nov
Day5 - SpringBootApplication Annotation - Coding RestAPI Around Resources - 8nov
Spring Boot...
IDE - STS
local machine.
clean spring-boot:run
Postman
AWS
clean package
Spring framework
Spring Boot
readmade, rapid application dev
starters
auto config
embedded tomcat
application.property
AutoConfig
Convert AppReq java object into Json string using Google Gson library.
jar file
OOPs
-======
Class
fields
methods
mvn package
jar file
======
all logic
@Configuration
@Bean
spring-boot-autoconfig
if we don't configure, then spring boot will run autoconfig, & places bean inside container..
=======
@ComponentScan
Spring boot uses this configuration for creating spring beans..
we define packages...
@SpringBootApplication
public class MyFirstAppProjApplication {
All classes defined in com.mycomp.myfirstapp package & sub packages, are scanned for
making spring bean.
===========
1. @SpringBootApplication
2. SpringApplication.run(MyFirstAppProjApplication.class, args);
3. @Configuration
4. @EnableAutoConfiguration
5. @ComponentScan
4. Reads @EnableAutoConfiguration
scan classpath, and wherever objects are needed, it creates it & adds to spring conatiner
RestAPIs
"Concept of Resource"
Student
Employee
Company
Payment
Mobile
Laptop
In order to build Rest API, you mandatory should identify resource, and write logic around it.
Payment - id
2 category:
1. CRUD - Create, read, update, delete
2. Other actions - other functionality
API: URL
http://localhost:8081/add
Based on the provided endpoint, spring boot, should read the endpoint & call corresponding
method
url
http://localhost:8081/add
<base url -------><URI>
POST
GET
PUT
PATCH
DELETE
Payment
1. CRUD -
Create, - create resource logic
POST /payments
request data
10 fields
response:
id
GET /payments/{id}
Response data
update,
PUT /payments/{id}
Request body
{
all 10 fields
}
PATCH /payments/{id}
Request body
{
1 field
}
delete
DELETE /payments/{id}
=====
POST /payments/{id}/repeat
===
DELETE /employees/{id}
PUT/PATH /employees/{id}
POST /checkout/orders
in request body, I should give all data needed to create order..
id
GET /checkout/orders/{id}
Response object
GET /checkout/orders/{id}
Other functionality
POST /checkout/orders/{id}/confirm-payment-source
creating restcontroller
https://chatgpt.com/share/672db10b-862c-8013-bec0-a8fcb666a7e2
@RestController
@RequestMapping("/v1/payments")
@PostMapping,Get, XxxMapping
@RequestBody
@PathVariable
/v1/payments
@PutMapping("/{id}")
updatePayment(@PathVariable long id, @ResponseBody data)