CHAPTER 4D spring Exception
CHAPTER 4D spring Exception
Handling
e
x
c
e
p
Spring REST Exception
Handling
Bad REST
data
Servi
/api/students/9999
ce
RES T
T h
Excepti r
Clie on
o
Handle
nt r
w
e
x
c
e
p
Spring REST Exception
Handling
Bad REST
data
Servi
/api/students/9999
ce
RES T
T h
Excepti r
Clie on
o
Handle
nt r
w
e
x
c
e
p
Development Step-
By-St
ep
Process
Development Step-
By-St
ep
Process
1. Create a
custom error response
class
Process
1. Create a custom error
response class
Process
1. Create a custom error response class
Process
1. Create a custom error response class
private
Step 1: Create custom error
response class
File: StudentErrorResponse.java
private
// constructors
// getters / setters
}
Step 2: Create custom student
exception
Step 2: Create custom student
exception
• The custom student exception will used by our
REST service
Step 2: Create custom student
exception
• The custom student exception will used by our
REST service
}
Call super
class
constructo
r
Step 3: Update REST service to throw
exception
Step 3: Update REST service to throw
exception
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
Step 3: Update REST service to throw
exception
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
@GetMapping("/students/{studentId}")
public Student getStudent(@PathVariable int studentId) {
Step 3: Update REST service to throw
exception
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
@GetMapping("/students/{studentId}")
public Student getStudent(@PathVariable int studentId) {
Throw
exception
Step 3: Update REST service to throw
exception
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class Could also
StudentRestController {
@GetMapping("/students/{studentId}") check
public Student getStudent(@PathVariable int studentId) { results
// check the studentId against list size from DB
if ( (studentId >= theStudents.size()) || (studentId < 0) ) {
throw new StudentNotFoundException("Student id notfound - " + studentId);
}
Throw
exception
Step 3: Update REST service to throw
exception
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
@GetMapping("/students/{studentId}")
public Student getStudent(@PathVariable int studentId) {
return theStudents.get(studentId);
}
Throw
… exception
}
Happy
path
Spring REST Exception
Handling
REST
Servi
/api/students/9999
ce
RES
T
Clie
nt
Spring REST Exception
Handling
Bad REST
data
Servi
/api/students/9999
ce
RES
T
Clie
nt
Spring REST Exception
Handling
Bad REST
data
Servi
/api/students/9999
ce
RES T
T h
r
Clie o
nt w
e
x
c
e
p
Spring REST Exception
Handling
Bad REST
data
Servi
/api/students/9999
ce
RES T
T h
Excepti r
Clie on
o
Handle
nt r
w
e
x
c
e
p
Step 4: Add exception handler
method
Step 4: Add exception handler
method
• Define exception handler method(s) with @ExceptionHandler
annotation
Step 4: Add exception handler
method
• Define exception handler method(s) with @ExceptionHandler
annotation
@RestController
@RequestMapping("/api")
public class
StudentRestController {
…
Step 4: Add exception handler
method
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
…
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
Step 4: Add exception handler
method
File: StudentRestController.java
Exception
@handler
RestC on t r o
m e t h
@RequestMapping("/api")
public class StudentRestController {
ol…dler
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
Step 4: Add exception handler
method
File: StudentRestController.java
Exception
Type of
the
response
@handler
RestC on t r o
m e t h
@RequestMapping("/api") body
public class StudentRestController {
ol…dler
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
Step 4: Add exception handler
method Exception
File: StudentRestController.java
Exception
Type of
the
response type
to handle /
@handler
RestC on t r o
m e t h
@RequestMapping("/api") body catch
public class StudentRestController {
ol…dler
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
Step 4: Add exception handler
method
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
…
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
@RestController
@RequestMapping("/api")
public class
StudentRestController {
…
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
@RestController
@RequestMapping("/api")
public class
StudentRestController {
…
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
error.setStatus(HttpStatus.NOT_FOUND.value());
Step 4: Add exception handler
method
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
…
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
error.setStatus(HttpStatus.NOT_FOUND.value());
error.setMessage(exc.getMessage());
Step 4: Add exception handler
method
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
…
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
error.setStatus(HttpStatus.NOT_FOUND.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());
Step 4: Add exception handler
method
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
…
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
error.setStatus(HttpStatus.NOT_FOUND.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());
Step 4: Add exception handler
method
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
…
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
error.setStatus(HttpStatus.NOT_FOUND.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());
}
Step 4: Add exception handler
method
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
…
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
error.setStatus(HttpStatus.NOT_FOUND.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());
} Bod
y
Step 4: Add exception handler
method
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
…
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
error.setStatus(HttpStatus.NOT_FOUND.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());
} Bod Status
y code
Step 4: Add exception handler
method
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
…
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
error.setStatus(HttpStatus.NOT_FOUND.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());
} Bod Status
y code
Spring REST Exception
Handling
REST
Servi
/api/students/9999
ce
RES
T
Clie
nt
Spring REST Exception
Handling
Bad REST
data
Servi
/api/students/9999
ce
RES
T
Clie
nt
Spring REST Exception
Handling
Bad REST
data
Servi
/api/students/9999
ce
RES T
T h
r
Clie o
nt w
e
x
c
e
p
Spring REST Exception
Handling
Bad REST
data
Servi
/api/students/9999
ce
RES T
T h
Excepti r
Clie on
o
Handle
nt r
w
e
x
c
e
p
Spring REST - Global Exception
Handling
e
x
c
e
p
It works,
but …
It works,
but …
• Exception handler code is only for the specific REST
controller
It works,
but …
• Exception handler code is only for the specific REST
controller
• Pre-process requests to
controllers
Spring
@ControllerAdvice
• @ControllerAdvice is similar to an interceptor /
filter
• Pre-process requests to
controllers
• Pre-process requests to
controllers
• Pre-process requests to
controllers Real-time
use of
• Post-process responses to handle AOP
exceptions
RES
T
Clie
nt
Spring REST Exception
Handling
Controll REST
er Servi
Advice
ce
/api/students/9999
RES
T
Clie
nt
Spring REST Exception
Handling
Controll REST
er Servi
Advice
ce
/api/students/9999
RES
T
Clie
nt
Spring REST Exception
Handling
Controll REST
er Servi
Advice
ce
/api/students/9999
RES
T Throw
exception
Clie
nt
Spring REST Exception
Handling
Controll REST
er Servi
Advice
ce
/api/students/9999
RES
T Throw
exception
Clie
nt
Spring REST Exception
Handling
Controll REST
er Servi
Advice
ce
/api/students/9999
RES
T Excepti Throw
exception
on
Clie Handler
(s)
nt ..
.
..
.
Spring REST Exception
Handling
Controll REST
er Servi
Advice
ce
/api/students/9999
RES
T Excepti Throw
exception
on
Clie Handler
(s)
nt ..
.
..
.
Spring REST Exception
Handling
Controll REST
er Servi
Advice
ce
/api/students/9999
RES
T Excepti Throw
exception
on
Clie Handler
(s)
nt ..
.
..
.
Development Step-
By-St
ep
Process
Development Step-
By-St
ep
Process
1. Create new
@ControllerAdvice
Process
1. Create new @ControllerAdvice
Process
1. Create new @ControllerAdvice
@ControllerAdvice
public class StudentRestExceptionHandler {
}
Step 2: Refactor - remove exception
handling
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
…
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
error.setStatus(HttpStatus.NOT_FOUND.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());
}
Step 2: Refactor - remove exception
handling
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
StudentRestController {
…
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
error.setStatus(HttpStatus.NOT_FOUND.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());
}
Step 2: Refactor - remove exception
handling
File: StudentRestController.java
@RestController
Remo
@RequestMapping("/api") ve
this
public class
StudentRestController { code
…
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
error.setStatus(HttpStatus.NOT_FOUND.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.currentTimeMillis());
}
Step 3: Add exception handler to
@ControllerAdvice
File: StudentRestExceptionHandler.java
@ControllerAdvice
public class StudentRestExceptionHandler {
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
error.setStatus(HttpStatus.NOT_FOUND.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.
());
@ControllerAdvice
public class StudentRestExceptionHandler {
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
error.setStatus(HttpStatus.NOT_FOUND.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.
());
@ExceptionHandler
public ResponseEntity<StudentErrorResponse> handleException(StudentNotFoundException exc) {
error.setStatus(HttpStatus.NOT_FOUND.value());
error.setMessage(exc.getMessage());
error.setTimeStamp(System.
());
RES
T
Clie
nt
Spring REST Exception
Handling
Controll REST
er Servi
Advice
ce
/api/students/9999
RES
T
Clie
nt
Spring REST Exception
Handling
Controll REST
er Servi
Advice
ce
/api/students/9999
RES
T
Clie
nt
Spring REST Exception
Handling
Controll REST
er Servi
Advice
ce
/api/students/9999
RES
T Throw
exception
Clie
nt
Spring REST Exception
Handling
Controll REST
er Servi
Advice
ce
/api/students/9999
RES
T Throw
exception
Clie
nt
Spring REST Exception
Handling
Controll REST
er Servi
Advice
ce
/api/students/9999
RES
T Excepti Throw
exception
on
Clie Handler
(s)
nt ..
.
..
.
www.luv2code.co
m
Spring REST Exception
Handling
Controll REST
er Servi
Advice
ce
/api/students/9999
RES
T Excepti Throw
exception
on
Clie Handler
(s)
nt ..
.
..
.
Spring REST Exception
Handling
Controll REST
er Servi
Advice
ce
/api/students/9999
RES
T Excepti Throw
exception
on
Clie Handler
(s)
nt ..
.
..
.
Spring REST API
Design
Process
API Design Step-
By-St
ep
Process
1. Review API
requirements
Process
1. Review API requirements
2. Identify main
resource / entity
API Design Step-
By-St
ep
Process
1. Review API requirements
/api/customers
Step 3: Use HTTP methods to assign action
on resource
Step 3: Use HTTP methods to assign action
on resource
HTTP Method CRUD Action
Step 3: Use HTTP methods to assign action
on resource
HTTP Method CRUD Action
POST Create a new entity
Step 3: Use HTTP methods to assign action
on resource
HTTP Method CRUD Action
POST Create a new entity
GET Read a list of entities or single entity
Step 3: Use HTTP methods to assign action
on resource
HTTP Method CRUD Action
POST Create a new entity
GET Read a list of entities or single entity
PUT Update an existing entity
Step 3: Use HTTP methods to assign action
on resource
HTTP Method CRUD Action
POST Create a new entity
GET Read a list of entities or single entity
PUT Update an existing entity
DELETE Delete an existing entity
Step 3: Use HTTP methods to assign action
on resource
HTTP Method CRUD Action
POST Create a new entity
GET Read a list of entities or single entity
PUT Update an existing entity
DELETE Delete an existing entity
Full
CRUD
CRUD Endpoint
Examples
CRUD Endpoint
Examples
HTTP Method Endpoint CRUD Action
CRUD Endpoint
Examples
HTTP Method Endpoint CRUD Action
POST /api/customers Create a new customer
CRUD Endpoint
Examples
HTTP Method Endpoint CRUD Action
POST /api/customers Create a new customer
GET /api/customers Read a list of customers
CRUD Endpoint
Examples
HTTP Method Endpoint CRUD Action
POST /api/customers Create a new customer
GET /api/customers Read a list of customers
GET /api/customers/{customerId} Read a single customer
CRUD Endpoint
Examples
HTTP Method Endpoint CRUD Action
POST /api/customers Create a new customer
GET /api/customers Read a list of customers
GET /api/customers/{customerId} Read a single customer
PUT /api/customers Update an existing customer
CRUD Endpoint
Examples
HTTP Method Endpoint CRUD Action
POST /api/customers Create a new customer
GET /api/customers Read a list of customers
GET /api/customers/{customerId} Read a single customer
PUT /api/customers Update an existing customer
DELETE /api/customers/{customerId} Delete an existing customer
CRUD Endpoint
Examples
HTTP Method Endpoint CRUD Action
POST /api/customers Create a new customer
GET /api/customers Read a list of customers
GET /api/customers/{customerId} Read a single customer
PUT /api/customers Update an existing customer
DELETE /api/customers/{customerId} Delete an existing customer
/api/customersList
/api/deleteCustomer
/api/addCustomer
/api/updateCustomer
Anti-
Patterns
• DO NOT DO THIS … these are REST anti-patterns, bad
practice
/api/customersList
/api/deleteCustomer
/api/addCustomer
/api/updateCustomer
/api/customersList
/api/deleteCustomer
/api/addCustomer
/api/updateCustomer Instead, use
HTTP
methods to
Don't include actions in the assign
endpoint
actions
CRM Real-Time
Project
CRM Real-Time
Project
HTTP Method Endpoint CRUD Action
POST /api/customers Create a new customer
GET /api/customers Read a list of customers
GET /api/customers/{customerId} Read a single customer
PUT /api/customers Update an existing customer
DELETE /api/customers/{customerId} Delete an existing customer
CRM Real-Time
Project
HTTP Method Endpoint CRUD Action
POST /api/customers Create a new customer
GET /api/customers Read a list of customers
GET /api/customers/{customerId} Read a single customer
PUT /api/customers Update an existing customer
DELETE /api/customers/{customerId} Delete an existing customer
CRM
Service
(spring-rest)
More API
Examples
More API
Examples
• On the following slides, we’ll look at APIs from other real-
time projects
More API
Examples
• On the following slides, we’ll look at APIs from other real-
time projects
• PayPa
l
More API
Examples
• On the following slides, we’ll look at APIs from other real-
time projects
• PayPa
l
• GitHu
b
More API
Examples
• On the following slides, we’ll look at APIs from other real-
time projects
• PayPa
l
• GitHu
b
• SalesFor
ce
PayP
al
PayP
al
• PayPal Invoicing
API
PayP
al
• PayPal Invoicing
API
• https://developer.paypal.com/docs/api/invoicing/
PayP
al
• PayPal Invoicing
API
• https://developer.paypal.com/docs/api/invoicing/
GitHu
b
GitHu
b
• GitHub Repositories
API
GitHu
b
• GitHub Repositories
API
• https://developer.github.com/v3/repos/#repositories
GitHu
b
• GitHub Repositories
API
• https://developer.github.com/v3/repos/#repositories
Create an individual
POST /services/apexrest/clinic01/v1/individual/
Update an individual
PUT /services/apexrest/clinic01/v1/individual/
Spring REST API - Real Time
Project
CRM
Service
(spring-rest)
Application
Architecture
CRM
Service
(spring-rest)
Application
Architecture
CRM
Customer Customer
Service
Service DAO
(spring-rest)
Application Reuse code from
Architecture
previous CRM
web project
CRM
Customer Customer
Service
Service DAO
(spring-rest)
Application Reuse code from
Architecture
previous CRM
web project
CRM
Customer Customer
Service
Service DAO
(spring-rest)
Process
Development Step-
By-St
ep
Process
Development Step-
By-St
ep
Process
1. Get
customers
Process
1. Get customers
2. Get single
customer by
ID
Development Step-
By-St
ep
Process
1. Get customers
2. Get single
customer by
ID
Process
1. Get customers
2. Get single
customer by
ID
4. Update an existing
customer
Development Step-
By-St
ep
Process
1. Get customers
2. Get single
customer by
ID
4. Update an existing
customer
5. Delete an existing
customer
Code Review Step-
By-St
ep
1. pom.xml file
5. DAO: CustomerDAO …
6. Service: CustomerService …
Spring CRM REST - Get
Customers
RES
T
Clie
nt
Application
Interaction
Custom
er
REST
RES Controll
T er
Clie
nt
Application
Interaction
Custom
er
GET /api/customers
REST
RES Controll
T er
Clie
nt
Application
Interaction
Custom
er
GET /api/customers
REST
RES Controll
T er
Clie
nt
Application
Architecture
Application
Architecture
Customer Customer
Service DAO
Application
Architecture
Customer
Customer Customer
REST
Service DAO
Controller
Application
Architecture
Customer
Customer Customer
REST
Service DAO
Controller
Process
Development Step-
By-St
ep
Process
1. Create Customer REST
Controller
Process
1. Create Customer REST
Controller
2. Autowire CustomerService
Development Step-
By-St
ep
Process
1. Create Customer REST
Controller
2. Autowire CustomerService
@RestController
@RequestMapping("/api"
)
public class
CustomerRestController
// autowire the CustomerService
{ @Autowired
private CustomerService
customerService;
} return
customerService.getCustomers();
}
Step 2: Autowire
CustomerService
File: StudentRestController.java
@RestController
@RequestMapping("/api"
)
public class
CustomerRestController
// autowire the CustomerService
{ @Autowired
private CustomerService
customerService;
return
customerService.getCustomers();
} }
Step 2: Autowire
CustomerService
File: StudentRestController.java
@RestController
@RequestMapping("/api"
)
public class
CustomerRestController
{
// autowire the
CustomerService @Autowired
// add mapping
private for GET /customers
CustomerService
customerService;
@GetMapping("/customers")
public List<Customer> getCustomers() {
return
customerService.getCustomers();
} }
Step 3: Add mapping for GET
/customers
File: StudentRestController.java
@RestController
@RequestMapping("/api"
)
public class
CustomerRestController
{
// autowire the
CustomerService @Autowired
// add mapping
private for GET /customers
CustomerService
customerService;
@GetMapping("/customers")
public List<Customer> getCustomers() {
return
customerService.getCustomers();
} }
Step 3: Add mapping for GET
/customers
File: StudentRestController.java
@RestController
@RequestMapping("/api"
)
public class
CustomerRestController
{
// autowire the
CustomerService @Autowired
private CustomerService
customerService;
return
Spring CRM REST - Get Single
Customer
RES
T
Clie
nt
Application
Interaction
Custom
er
REST
RES
Controll
T
er
Clie
nt
Application
Interaction
Custom
GET /api/customers/{customerId}
er
REST
RES
Controll
T
er
Clie
nt
Application
Interaction
Custom
GET /api/customers/{customerId}
er
REST
RES
Controll
T
er
Clie
nt
Get Single
Customer
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
CustomerRestController
{
return theCustomer;
}
}
Get Single
Customer
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
CustomerRestController
{
return theCustomer;
}
}
Get Single
Customer
File: StudentRestController.java
@RestController
@RequestMapping("/api")
public class
CustomerRestController
{
@GetMapping("/customers/{customerId}")
public Customer getCustomer(@PathVariable int
customerId) {
return theCustomer;
}
}
Spring CRM REST - Exception
Handling
RES
T
Clie
nt
Application
Interaction
CR
M
RES
RES T
T Contr
oller
Clie
nt
Application
Interaction
CR
M
GET /api/customers/9999
RES
RES T
T Contr
oller
Clie
nt
Application
Interaction Bad
Data CR
M
GET /api/customers/9999
RES
RES T
T Contr
oller
Clie
nt
Application
Interaction Bad
Data Controll CR
er M
GET /api/customers/9999
Advice RES
RES T
T Contr
oller
Clie
nt
Application
Interaction Bad
Data Controll CR
er M
GET /api/customers/9999
Advice RES
RES T
T Contr
oller
Clie
nt
Application
Interaction Bad
Data Controll CR
er M
GET /api/customers/9999
Advice RES
RES T
T Contr
oller
Clie
nt Throw
exception
Application
Interaction Bad
Data Controll CR
er M
GET /api/customers/9999
Advice RES
RES T
T Contr
oller
Clie
nt Throw
exception
Application
Interaction Bad
Data Controll CR
er M
GET /api/customers/9999
Advice RES
RES T
T Excepti
Contr
on oller
Clie Handler
(s)
nt ..
.
Throw
exception
..
.
Application
Interaction Bad
Data Controll CR
er M
GET /api/customers/9999
Advice RES
RES T
T Excepti
Contr
on oller
Clie Handler
(s)
nt ..
.
Throw
exception
..
.
Global Exception
Handler(s)
Application
Interaction Bad
Data Controll CR
er M
GET /api/customers/9999
Advice RES
RES T
T Excepti
Contr
on oller
Clie Handler
(s)
nt ..
.
Throw
exception
..
.
Global Exception
Handler(s)
Development Step-
By-St
ep
Process
Development Step-
By-St
ep
Process
1. Create a
custom error response
class
Process
1. Create a custom error
response class
Process
1. Create a custom error response class
Process
1. Create a custom error response class