https://youtube.com/playlist?list=PLzdlNxYnNoaf5bb-Pwb7MbWHGlRf28pVO&si=fUuG6enQWCokfN4t
Annotation Type | Spring Boot | Quarkus |
---|---|---|
Main Class | @SpringBootApplication | @QuarkusMain |
Dependency Inj. | @Autowired | @Inject |
Components | @Repository | @ApplicationScoped |
@Service | @ApplicationScoped | |
@Component | @ApplicationScoped | |
Data Access | CrudRepository | PanacheRepository |
@Entity | @Entity | |
@Transactional | @Transactional | |
Web | @RestController | @Path |
@GetMapping | @GET | |
@PostMapping | @POST | |
Testing | @SpringBootTest | @QuarkusTest |
@MockBean | @InjectMock | |
Configuration | @Value | @ConfigProperty |
Caching | @EnableCaching | @CacheResult |
Scheduling | @Scheduled | @Scheduled |
Async | @Async | @Blocking/@NonBlocking |
- Support both
Imperative -> if ABC data fetched, it will search entire DB and bring record
Reactive -> If ABC is fetched, it will return data 1 by 1 as it gets.
-
Open source
-
Set JAVA_HOME
-
Download Maven https://maven.apache.org/download.cgi > Binary zip archive > keep in C:\Program Files (x86) > set path (C:\Program Files (x86)\apache-maven-3.9.11\bin) in "PATH"
-
Dependency Selected
- RESTEasy Classic
- RESTEasy Classic Jackson
-
Add project to intellij
-
Add plugin in Intellij as > Quarkus Tools
-
run Project with command
mvn quarkus:dev
OR./mvnw quarkus:dev
(Ref : Quarkus_README.md ) -
In 4th Lecture
-
To check Quarkus supported Extension/Dependencies ->
mvn quarkus:list-extensions
OR./mvnw quarkus:list-extensions
-
To Add >
./mvnw quarkus:add-extension -Dextensions="_____"
ORmvn quarkus:add-extension -Dextensions="Mutiny"
-
In 6th Part - All 4 API added MobileResource.java and their Postman collection quarkus-tutorial-mvn.postman_collection.json
-
In 7th Part - All 4 API updated with modal and 5th API ByNumber (Mobile.java) and new resource MobileResourcePart7.java and their Postman collection quarkus-tutorial-mvn.postman_collection.json
-
In 8th Part -
- Add swagger
mvn quarkus:add-extension -Dextensions="quarkus-smallrye-openapi"
- Goto http://localhost:8080/q/swagger-ui/
- To Change default swagger path (http://localhost:8080/q/swagger-ui/) to custom (http://localhost:8080/swag/)
quarkus.swagger-ui.path=/swag
- To disable swagger UI Path
quarkus.swagger-ui.always-include=false
- Add swagger
-
In 9th Part - Bring data from other Micro service
- Add following extension/dependency :
- "REST Client" to call remote API
mvn quarkus:add-extension -Dextensions="quarkus-rest-client"
- "REST Client jackson" to call remote API
mvn quarkus:add-extension -Dextensions="quarkus-rest-client-jackson"
- "quarkus-rest-jackson" to call remote API
mvn quarkus:add-extension -Dextensions="quarkus-rest-jackson"
- Remove quarkus-resteasy and quarkus-resteasy-jackson due to conflict regards ( Caused by: jakarta.enterprise.inject.spi.DeploymentException: Mixing Quarkus REST and RESTEasy Classic server parts is not supported )
- "REST Client" to call remote API
- Use DUMMY client as https://www.tvmaze.com/api
- Search TV show by Id - https://api.tvmaze.com/shows/169
- Create Modal class for above response
- Add following extension/dependency :
-
In Part-10 : cors ( Cross Origin Resource Sharing ) - allow external service to access our service ( Open this from Browser : Part-10.html )
quarkus.http.cors=true quarkus.http.cors.origins=*
- Allow only specific method
quarkus.http.cors.methodshs=GET, POST
-
In Part-11, 12 : Microprofile Fault Tolerance
-
Add following extension/dependency :
- "Fault Tolerance" to call default method if remote API offline
mvn quarkus:add-extension -Dextensions="quarkus-smallrye-fault-tolerance"
- "Fault Tolerance" to call default method if remote API offline
-
Create Fallback Method
@Fallback(fallbackMethod = "getTvSeriesByIdFallback")
- Retry
@Retry(maxRetries = 2)
- Timeout
@Timeout(1000)
- Circuit Breaker
@CircuitBreaker( requestVolumeThreshold=2, failureRatio=0.5, delay = 10, delayUnit = ChronoUnit.SECONDS )
- Git Bash Script to hit URL in loop
while true; do sleep 1; curl http://localhost:8080/tvseries/260; echo -e '\n'; done
- Part-13, 14 : Hibernate-ORM PanacheEntity with H2DB
- "H2DB" -
./mvnw quarkus:add-extension -Dextensions="io.quarkus:quarkus-jdbc-h2"
- "Hibernate ORM" -
./mvnw quarkus:add-extension -Dextensions="io.quarkus:quarkus-hibernate-orm-panache"
- Two Types
- Extend Entity Class with PanacheEntity
- Implement with PanacheRepository
- Mapping
-
One to One
-
Add
@OneToOne
to secondary classTable 1 T1_ID T1_Name Table 2 T2_ID T2S_Name T1_ID -
But here we can only find Table 1 from Table 2, So
-
We will add
@OneToOne
to Primary class
- But now we have two column on both table - So to remove extra column from Primary class use in Primary class : ```@OneToOne(mappedBy = "<*Primary class*>")``` - To fetch both at once use Primary class : **fetch = FetchType.EAGER** : ```@OneToOne(mappedBy = "<*Primary class*>", fetch = FetchType.EAGER)``` - To fix ```Infinite recursion``` - which is due to both class has ```@OneToOne``` annotation 1. Add @JsonManagedReference in Primary class 2. Add @JsonBackReference in secondary class - To save both at Once use in Primary class : **cascade = CascadeType.ALL** : ```@OneToOne(mappedBy = "laptop", fetch = FetchType.EAGER, cascade = CascadeType.ALL)```Table 1 T1_ID T1_Name T2_ID Table 2 T2_ID T2S_Name T1_ID -
-
- "H2DB" -
-
-
Part 29 : Logging
- Info - Method info
- Debug - Data Info
- warning
- Error
-
Part- 30 : Exception -TODO
-
Part - 32 : MicroProfile Health
- Add
mvn quarkus:add-extension -Dextensions="quarkus-smallrye-health"
- Two Type
- Liveness LivenessHealthCheck.java - To check Proxy/Client Connections
- Readiness ReadinessHealthCheck.java - To check DB connection
- Test URLs
- Add
-
Part-33 How to use MicroProfile Metrics
-
Add
mvn quarkus:add-extension -Dextensions="quarkus-smallrye-metrics"
-
Types Number.java
- @Counted
{ "com.learn.resource.part33_microProfile_matrics.Number.Count_checkIfPrimeNumber": 3 }
- @Timed
{ "com.learn.resource.part33_microProfile_matrics.Number.Time_checkIfPrimeNumber": { "p99": 319201.0, "min": 15200.0, "max": 319201.0, "mean": 156003.43605748552, "p50": 15600.0, "p999": 319201.0, "stddev": 142029.00004744556, "p95": 319201.0, "p98": 319201.0, "p75": 280601.0, "fiveMinRate": 0.2032510706679223, "fifteenMinRate": 0.2011018917421949, "meanRate": 0.23204665475695216, "count": 4, "oneMinRate": 0.21471253794774184, "elapsedTime": 630602.0 } }
- @Metered
{ "com.learn.resource.part33_microProfile_matrics.Number.Metered_checkIfPrimeNumber": { "fiveMinRate": 0.38704854970388447, "fifteenMinRate": 0.39559850366986254, "meanRate": 0.1737586528638574, "count": 4, "oneMinRate": 0.34222396825043916 } }
- @Gauge(unit = MetricUnits.MILLISECONDS) - For Custom
{ "com.learn.resource.part33_microProfile_matrics.Number.getHighestInputPrimeNumber": 2 }
-
🧪 Test URLs - http://localhost:8080/q/metrics/application
-
-
Part-34 🔐 Authentication and Authorization using JWT Token and Roles-Based Access Control
- Create two Module
- App (Course) - All APIs - course
- Run
cd .\course mvn quarkus:add-extension -Dextensions="quarkus-smallrye-jwt"
- Add in application.properties
mp.jwt.verify.issuer=jwt-token mp.jwt.verify.publickey.location=../jwt/publicKey.pem
- On Method Add
@RolesAllowed({"teacher","admin"})
- Run
- JWT - Generate Token - JWT
- Run
cd .\JWT mvn quarkus:add-extension -Dextensions="quarkus-smallrye-jwt" mvn quarkus:add-extension -Dextensions="quarkus-smallrye-jwt-build"
- Add in application.properties
smallrye.jwt.sign.key.location=../jwt/privateKey.pem
- Run
- Generate Key 🔐
- Open GitBash
- Run
mkdir jwt openssl genrsa -out jwt/rsaPrivateKey.pem 2048 openssl rsa -pubout -in jwt/rsaPrivateKey.pem -out jwt/publicKey.pem openssl pkcs8 -topk8 -nocrypt -inform pem -in jwt/rsaPrivateKey.pem -outform pem -out jwt/privateKey.pem chmod 600 jwt/rsaPrivateKey.pem chmod 600 jwt/privateKey.pem
- Folder will be created as jwt ( jwt ), and it will contain following files
- Fix
ignored POM.XML
file issue : Goto Settings > Build, Execution > Build Tools > Maven > IgnoreFiles > Untick Both Module - Validate barrier key in https://www.jwt.io/
- App (Course) - All APIs - course
- Create two Module
-
Part-35 🔐 Authentication and Authorization using Keycloak Server
- Goto https://www.keycloak.org/ and download > extract and goto bin folder
- Open bin folder in cmd, run
kc.bat start-dev
- Create New Realm(branch) by clinking on
master
i create asiit
- Now goto Client and create New client -
- I created clientID and name as
iit-pune
- Client Type = OpenId
- Turn on Client Authentication and Authorization
- ✅ Tick on Flow > Standard Flow, Direct Access Flow
- Add java URL in > Root, Home
- I created clientID and name as
- Now Roles to Client > click client 'iit-pune' > Roles Tab > Create Role
- Now Create User > fill details > Role Mapping > add
- Add Role to Java Method with
@RolesAllowed({"student","professor","admin"})
- Run
cd .\part-35-Keycloa mvn quarkus:add-extension -Dextensions="quarkus-oidc" mvn quarkus:add-extension -Dextensions="quarkus-keycloak-authorization"
- Add in application.properties
quarkus.oidc.auth-server-url=http://localhost:8080/realms/iit quarkus.oidc.client-id=iit-pune quarkus.oidc.credentials.secret=Ib1ua1DwqAAjLscpLmF6n66uWbWkpVIg quarkus.oidc.authentication.user-info-required=true
-
Part-36 How to use Lombok library
⚠️ Never use @DATA to Entity Class - it will give exception or @Oneto*** due to circular ref- ✨ @Data Contains every thing like @Getter, @Setter, @AllArgConst , @NoArgCons
-
Part-38 How to test secured Api
- Add dependency
mvn quarkus:add-extension -Dextensions="quarkus-test-security"
- Test Class needs to be annotated with
@QuarkusTest
and@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
- Method with
@Test
, Optional@Order(1)
- JWT security can be disabled by two ways: MobileResourceTest.java
- @TestSecurity(authorizationEnabled = false)
- @TestSecurity(user = "test", roles = "student")
- Add dependency
-
Part-37 How to convert Entity(DAO) >> DTO(Modal)(Data Transfer Object)
⚠️ Issue is if we modify data in DAO after fetching and method is annotated with @Transactional then modified data gets saved in DB.- Add dependency
MapStruct, MapStruct-Processor, lombok-mapstruct-binding
from MVN
-
Part-40 How to use Hibernate Validator Extension to validate Entities
- Add dependency
mvn quarkus:add-extension -Dextensions="quarkus-hibernate-validator"
- Add Annotation to Entity (TODO : Need to check for DTO) Citizen.java
- Catch validation message - Ref Add Method - CitizenResource.java
- OR Add @VValid at input Method variable.
- Add dependency
-
Part-41 How to debug
- when
mvn clean quarkus:dev
then default debug port is 5005 - To start with Custom Port
mvn clean quarkus:dev -Ddebug=8121
- In Injellij > Run > Attach To Process > select port
- when