0% found this document useful (0 votes)
7 views

Day5 - SpringBootApplication Annotation - Coding RestAPI Around Resources - 8nov

Uploaded by

Prasanta Agasti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Day5 - SpringBootApplication Annotation - Coding RestAPI Around Resources - 8nov

Uploaded by

Prasanta Agasti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Diagrams:

Day 5 - API & Rest Standard..

Spring Boot...

1st spring boot application:


start.spring.io

IDE - STS

Build tool: Maven

local machine.
clean spring-boot:run

Postman

AWS
clean package

scp jar to destination

nohup java -jar nam.jar &

Security group configuration


------
Spring Boot

Spring framework
Spring Boot
readmade, rapid application dev

starters

auto config

embedded tomcat

application.property

non xml approach

AutoConfig

Gson - JSON strings.

java object => Json string

Convert AppReq java object into Json string using Google Gson library.

jar file

OOPs

-======
Class
fields
methods

mvn package
jar file
======
all logic

either you write logic, or

logic is written by someone else


jar files..

which jar file has what type of logic


which class in that jar hold your logic
which method to use..

@Configuration
@Bean

Gson object can be added into spring container..

Spring Boot Auto Config


Based on the jars which are there in classpath, SPring boot decides, should it
automatically create some objects as spring beans, & hold inside container..
So develepors can automatically use it using DI or DL

What happens, if we define @Bean

spring-boot-autoconfig

1. based on jar file in your class path..


2. Spring boot has already defined which classes should be autoconfigured..
3. @ConditionalOnMissingBean
If Gson bean is not in spring container,
then spring boot automatically creates Gson object & adds to container.. - autoconfig
- if Gson bean is already in container
if we explicity configure @Bean, then autoconfig will ot happen.

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.

You can explicitly configure


@ComponentScan(basePackages = {"com.mycomp.outsidepackage",
"com.mycomp.myfirstapp"})

===========

Flow of Spring Boot Application...

1. @SpringBootApplication
2. SpringApplication.run(MyFirstAppProjApplication.class, args);

3. @Configuration
4. @EnableAutoConfiguration
5. @ComponentScan

1. Creates MyFirstAppProjApplication object as Spring Bean and adds to container.


2. @ComponentScan
all classes available in com.mycomp.myfirstapp package & its sub packages.. spring
boot scans them for @Component & creates spring beans..
3. @Configuration, read @Bean for creating spring beans

4. Reads @EnableAutoConfiguration
scan classpath, and wherever objects are needed, it creates it & adds to spring conatiner

5. Dependency injections properly..

all beans would be created


our custom code beans @Component
predefined @Bean
AutoCOnfigured
Dependency injection

RestAPIs

Set of standards, of how to develop Web APIs.


And the APIs following Rest Standards is called as REST API, Restful web services

What are these Rest Standards..

"Concept of Resource"

Anything which can be uniquely identified with an ID, if called as a resource.

Student
Employee
Company
Payment
Mobile
Laptop

IN your project develop rest apis..


all your project functionality should be written in the form of resources..

Identify what resources are there in your project.

In order to build Rest API, you mandatory should identify resource, and write logic around it.

Payment - id

what business logic can be around resource??

2 category:
1. CRUD - Create, read, update, delete
2. Other actions - other functionality

repeate this payment again..

How to technical code it.

API: URL

http://localhost:8081/add

Based on the provided endpoint, spring boot, should read the endpoint & call corresponding
method

RPC based API

url
http://localhost:8081/add
<base url -------><URI>

For payment resource, finalize one URI


/payments

How to code logic CRUD / actions using Rest APIs


HTTP Verbs + Fixed URI

POST
GET
PUT
PATCH
DELETE

Payment

1. CRUD -
Create, - create resource logic

POST /payments
request data
10 fields
response:
id

Read, read resource logic

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}

update existing employee with id with datae coming in request body.

POST /checkout/orders
in request body, I should give all data needed to create order..

response should give ID back

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)

You might also like