SlideShare a Scribd company logo
1 of 23
Download to read offline
The one and only way
of designing Java
applications
Arturs Drozdovs (@Mordavolt)
Outline
●
API: REST vs RPC
●
Packages: Domain vs Functionality
●
Exceptions: Checked vs Runtime
●
Tests: Unit vs Functional
●
Frameworks and Libraries
API: REST vs RPC
●
REST
– Uses HTTP verbs as actions
– Can be traversed and manipulated intuitively (HATEOAS)
●
RPC
– Uses URIs as actions
– Has to be premediated
HATEOAS
{
"shippedToday": 20,
"_links": {
"self": {"href": "/orders"},
"next": {"href": "/orders?page=2"}
},
"_embedded": {
"ea:order": [{
"_links": {
"self": {"href": "/orders/123"},
"ea:basket": {"href": "/baskets/98712"}
},
"status": "shipped"}]
}
}
Create
POST /orders
{
"amount": 100
}
> 201 Created
> Location: orders/123
PUT /orders/123
{
"id": 123,
"amount": 100
}
> 200 OK | 204 No Content
POST /orders/create
POST /orders/add
POST /orders?action=create
POST /orders
{
"amount": 100
}
> ????
□ REST □ RPC
Read
GET /orders
>200 OK
>[
> {
> "id": 123,
> "amount": 100
> }
>]
GET /orders?amount=101
>200 OK
>[]
□ REST and RPC
GET /orders/123
>200 OK
>{
> "id": 123,
> "amount": 100
>}
GET /orders/555
>404 Not Found
Update
PUT /orders/123
{
"id": 123,
"amount": 777
}
> 200 OK | 204 No Content
PATCH /orders/123
Content-Type: application/json-patch+json
[
{ "op": "replace", "path": "/amount",
"value": 777 }
]
> 200 OK | 204 No Content
POST /orders/123/update
POST /orders/update?id=123
POST /orders?
action=update&id=123
POST /orders
{
"amount": 777
}
> ????
□ REST □ RPC
Delete
DELETE /orders/123
> 200 OK | 202 Accepted
| 204 No Content
GET /orders/123/delete
GET /orders/123?action=delete
POST /orders/123?action=delete
> ????
□ REST □ RPC
REST
●
Pros:
– Intuitive design
– API longevity
– Layered architecture (caching)
●
Cons:
– Steep entry curve
– Low short-term benefits
RPC
●
Pros:
– Easy entry
●
Cons:
– Hard to maintain
– Hard to traverse and operate on
– Hard to layer and abstract
Package naming
Singular Plural
Packages: Domain vs Functionality
Why package-private is cool?
●
Less coupling
●
Clean global scope
●
No service bypass
●
No mindless reuse
●
Easy to search for
related items
Exceptions: Checked vs Runtime
class SomeClass {
class CheckedException extends Exception {}
class UncheckedException extends RuntimeException {}
void fail() { throw new CheckedException(); }
void failSilently() { throw new UncheckedException(); }
}
All business logic exceptions should be checked!
2 rules of domain abstractions
Get Data
Some Data
No Data
Failure
ServiceA
ServiceB
GetA
GetB FailB
FailB
Example Exception Stack
@ResponseStatus
public abstract class OrderException extends Exception {
OrderException(String s) { super(s); }
OrderException(String s, Throwable t) { super(s, t); }
}
@ResponseStatus(NOT_FOUND)
class OrderNotFoundException extends OrderException {
OrderNotFoundException(String s) { super(s); }
OrderNotFoundException(String s, Throwable t) { super(s, t); }
}
Example Exception Usage
public Order getOrder(int id) throws OrderNotFoundException {
//return new Order();
throw new OrderNotFoundException("Order not found");
}
void doStuffWithOrder(int id) {
try {
service.getOrder(id);
} catch (OrderException e) {
e.printStackTrace();
}
}
Why does it fail? :(
●
Have to try-catch
●
Can’t throw another exception
●
No fail flow
What do?
●
Don’t use checked exceptions
●
Wrap with runtime exceptions
●
Use RxJava or Reactor
Tests: Unit vs Functional
●
Unit tests
– Test smallest possible unit of code
– Highly code dependant
– Quick
●
Functional tests
– Test all parts in conjunction
– API dependant
– Slow and inefficient
Frameworks and Libraries
●
Lombok
●
Mapstruct
●
OpenAPI Specification (Swagger)
… more stuff
●
Spring
– Actuator
– Sleuth
– Configuration Processor
●
Mockito
●
AssertJ
●
Flyway
●
Testcontainers
Questions?

More Related Content

What's hot

Structured Testing Framework
Structured Testing FrameworkStructured Testing Framework
Structured Testing Frameworkserzar
 
Concise at NTU Graduate Institute of Linguistics
Concise at NTU Graduate Institute of Linguistics Concise at NTU Graduate Institute of Linguistics
Concise at NTU Graduate Institute of Linguistics kuanming
 
Sql Objects And PL/SQL
Sql Objects And PL/SQLSql Objects And PL/SQL
Sql Objects And PL/SQLGary Myers
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQLVikash Sharma
 
Reactive features of MicroProfile you need to learn
Reactive features of MicroProfile you need to learnReactive features of MicroProfile you need to learn
Reactive features of MicroProfile you need to learnPayara
 

What's hot (9)

Structured Testing Framework
Structured Testing FrameworkStructured Testing Framework
Structured Testing Framework
 
Concise at NTU Graduate Institute of Linguistics
Concise at NTU Graduate Institute of Linguistics Concise at NTU Graduate Institute of Linguistics
Concise at NTU Graduate Institute of Linguistics
 
Ajax
AjaxAjax
Ajax
 
Sql Objects And PL/SQL
Sql Objects And PL/SQLSql Objects And PL/SQL
Sql Objects And PL/SQL
 
Sql Functions And Procedures
Sql Functions And ProceduresSql Functions And Procedures
Sql Functions And Procedures
 
SQL Server Stored procedures
SQL Server Stored proceduresSQL Server Stored procedures
SQL Server Stored procedures
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
Reactive features of MicroProfile you need to learn
Reactive features of MicroProfile you need to learnReactive features of MicroProfile you need to learn
Reactive features of MicroProfile you need to learn
 
Awr report error
Awr report errorAwr report error
Awr report error
 

Similar to The one and only way of designing Java applications

[245] presto 내부구조 파헤치기
[245] presto 내부구조 파헤치기[245] presto 내부구조 파헤치기
[245] presto 내부구조 파헤치기NAVER D2
 
Domain Driven Design Tactical Patterns
Domain Driven Design Tactical PatternsDomain Driven Design Tactical Patterns
Domain Driven Design Tactical PatternsRobert Alexe
 
Introduction to Elasticsearch
Introduction to ElasticsearchIntroduction to Elasticsearch
Introduction to ElasticsearchSperasoft
 
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...Data Con LA
 
Presto anatomy
Presto anatomyPresto anatomy
Presto anatomyDongmin Yu
 
Java colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rsJava colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rsSagara Gunathunga
 
Elasticsearch And Ruby [RuPy2012]
Elasticsearch And Ruby [RuPy2012]Elasticsearch And Ruby [RuPy2012]
Elasticsearch And Ruby [RuPy2012]Karel Minarik
 
Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaRxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaKros Huang
 
From Query Plan to Query Performance: Supercharging your Apache Spark Queries...
From Query Plan to Query Performance: Supercharging your Apache Spark Queries...From Query Plan to Query Performance: Supercharging your Apache Spark Queries...
From Query Plan to Query Performance: Supercharging your Apache Spark Queries...Databricks
 
"Scala in Goozy", Alexey Zlobin
"Scala in Goozy", Alexey Zlobin "Scala in Goozy", Alexey Zlobin
"Scala in Goozy", Alexey Zlobin Vasil Remeniuk
 
Scala45 spray test
Scala45 spray testScala45 spray test
Scala45 spray testkopiczko
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalystdwm042
 
Rapid API development examples for Impress Application Server / Node.js (jsfw...
Rapid API development examples for Impress Application Server / Node.js (jsfw...Rapid API development examples for Impress Application Server / Node.js (jsfw...
Rapid API development examples for Impress Application Server / Node.js (jsfw...Timur Shemsedinov
 
RESTful web service with JBoss Fuse
RESTful web service with JBoss FuseRESTful web service with JBoss Fuse
RESTful web service with JBoss Fuseejlp12
 
Android Automated Testing
Android Automated TestingAndroid Automated Testing
Android Automated Testingroisagiv
 
Evolving The Java Language
Evolving The Java LanguageEvolving The Java Language
Evolving The Java LanguageQConLondon2008
 
Ruby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrādeRuby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrādeRaimonds Simanovskis
 
Apache Spark in your likeness - low and high level customization
Apache Spark in your likeness - low and high level customizationApache Spark in your likeness - low and high level customization
Apache Spark in your likeness - low and high level customizationBartosz Konieczny
 
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationAjax Experience 2009
 
Tk2323 lecture 9 api json
Tk2323 lecture 9   api jsonTk2323 lecture 9   api json
Tk2323 lecture 9 api jsonMengChun Lam
 

Similar to The one and only way of designing Java applications (20)

[245] presto 내부구조 파헤치기
[245] presto 내부구조 파헤치기[245] presto 내부구조 파헤치기
[245] presto 내부구조 파헤치기
 
Domain Driven Design Tactical Patterns
Domain Driven Design Tactical PatternsDomain Driven Design Tactical Patterns
Domain Driven Design Tactical Patterns
 
Introduction to Elasticsearch
Introduction to ElasticsearchIntroduction to Elasticsearch
Introduction to Elasticsearch
 
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
Big Data Day LA 2015 - Compiling DSLs for Diverse Execution Environments by Z...
 
Presto anatomy
Presto anatomyPresto anatomy
Presto anatomy
 
Java colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rsJava colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rs
 
Elasticsearch And Ruby [RuPy2012]
Elasticsearch And Ruby [RuPy2012]Elasticsearch And Ruby [RuPy2012]
Elasticsearch And Ruby [RuPy2012]
 
Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaRxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJava
 
From Query Plan to Query Performance: Supercharging your Apache Spark Queries...
From Query Plan to Query Performance: Supercharging your Apache Spark Queries...From Query Plan to Query Performance: Supercharging your Apache Spark Queries...
From Query Plan to Query Performance: Supercharging your Apache Spark Queries...
 
"Scala in Goozy", Alexey Zlobin
"Scala in Goozy", Alexey Zlobin "Scala in Goozy", Alexey Zlobin
"Scala in Goozy", Alexey Zlobin
 
Scala45 spray test
Scala45 spray testScala45 spray test
Scala45 spray test
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
Rapid API development examples for Impress Application Server / Node.js (jsfw...
Rapid API development examples for Impress Application Server / Node.js (jsfw...Rapid API development examples for Impress Application Server / Node.js (jsfw...
Rapid API development examples for Impress Application Server / Node.js (jsfw...
 
RESTful web service with JBoss Fuse
RESTful web service with JBoss FuseRESTful web service with JBoss Fuse
RESTful web service with JBoss Fuse
 
Android Automated Testing
Android Automated TestingAndroid Automated Testing
Android Automated Testing
 
Evolving The Java Language
Evolving The Java LanguageEvolving The Java Language
Evolving The Java Language
 
Ruby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrādeRuby on Rails Oracle adaptera izstrāde
Ruby on Rails Oracle adaptera izstrāde
 
Apache Spark in your likeness - low and high level customization
Apache Spark in your likeness - low and high level customizationApache Spark in your likeness - low and high level customization
Apache Spark in your likeness - low and high level customization
 
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus Presentation
 
Tk2323 lecture 9 api json
Tk2323 lecture 9   api jsonTk2323 lecture 9   api json
Tk2323 lecture 9 api json
 

Recently uploaded

DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 

Recently uploaded (20)

DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 

The one and only way of designing Java applications

  • 1. The one and only way of designing Java applications Arturs Drozdovs (@Mordavolt)
  • 2. Outline ● API: REST vs RPC ● Packages: Domain vs Functionality ● Exceptions: Checked vs Runtime ● Tests: Unit vs Functional ● Frameworks and Libraries
  • 3. API: REST vs RPC ● REST – Uses HTTP verbs as actions – Can be traversed and manipulated intuitively (HATEOAS) ● RPC – Uses URIs as actions – Has to be premediated
  • 4. HATEOAS { "shippedToday": 20, "_links": { "self": {"href": "/orders"}, "next": {"href": "/orders?page=2"} }, "_embedded": { "ea:order": [{ "_links": { "self": {"href": "/orders/123"}, "ea:basket": {"href": "/baskets/98712"} }, "status": "shipped"}] } }
  • 5. Create POST /orders { "amount": 100 } > 201 Created > Location: orders/123 PUT /orders/123 { "id": 123, "amount": 100 } > 200 OK | 204 No Content POST /orders/create POST /orders/add POST /orders?action=create POST /orders { "amount": 100 } > ???? □ REST □ RPC
  • 6. Read GET /orders >200 OK >[ > { > "id": 123, > "amount": 100 > } >] GET /orders?amount=101 >200 OK >[] □ REST and RPC GET /orders/123 >200 OK >{ > "id": 123, > "amount": 100 >} GET /orders/555 >404 Not Found
  • 7. Update PUT /orders/123 { "id": 123, "amount": 777 } > 200 OK | 204 No Content PATCH /orders/123 Content-Type: application/json-patch+json [ { "op": "replace", "path": "/amount", "value": 777 } ] > 200 OK | 204 No Content POST /orders/123/update POST /orders/update?id=123 POST /orders? action=update&id=123 POST /orders { "amount": 777 } > ???? □ REST □ RPC
  • 8. Delete DELETE /orders/123 > 200 OK | 202 Accepted | 204 No Content GET /orders/123/delete GET /orders/123?action=delete POST /orders/123?action=delete > ???? □ REST □ RPC
  • 9. REST ● Pros: – Intuitive design – API longevity – Layered architecture (caching) ● Cons: – Steep entry curve – Low short-term benefits
  • 10. RPC ● Pros: – Easy entry ● Cons: – Hard to maintain – Hard to traverse and operate on – Hard to layer and abstract
  • 12. Packages: Domain vs Functionality
  • 13. Why package-private is cool? ● Less coupling ● Clean global scope ● No service bypass ● No mindless reuse ● Easy to search for related items
  • 14. Exceptions: Checked vs Runtime class SomeClass { class CheckedException extends Exception {} class UncheckedException extends RuntimeException {} void fail() { throw new CheckedException(); } void failSilently() { throw new UncheckedException(); } } All business logic exceptions should be checked!
  • 15. 2 rules of domain abstractions Get Data Some Data No Data Failure ServiceA ServiceB GetA GetB FailB FailB
  • 16. Example Exception Stack @ResponseStatus public abstract class OrderException extends Exception { OrderException(String s) { super(s); } OrderException(String s, Throwable t) { super(s, t); } } @ResponseStatus(NOT_FOUND) class OrderNotFoundException extends OrderException { OrderNotFoundException(String s) { super(s); } OrderNotFoundException(String s, Throwable t) { super(s, t); } }
  • 17. Example Exception Usage public Order getOrder(int id) throws OrderNotFoundException { //return new Order(); throw new OrderNotFoundException("Order not found"); } void doStuffWithOrder(int id) { try { service.getOrder(id); } catch (OrderException e) { e.printStackTrace(); } }
  • 18. Why does it fail? :( ● Have to try-catch ● Can’t throw another exception ● No fail flow
  • 19. What do? ● Don’t use checked exceptions ● Wrap with runtime exceptions ● Use RxJava or Reactor
  • 20. Tests: Unit vs Functional ● Unit tests – Test smallest possible unit of code – Highly code dependant – Quick ● Functional tests – Test all parts in conjunction – API dependant – Slow and inefficient
  • 22. … more stuff ● Spring – Actuator – Sleuth – Configuration Processor ● Mockito ● AssertJ ● Flyway ● Testcontainers