RESTful API - Exception Handling

Slack Discussion Repositories https://github.com/terawarehouse/terawarehouse-catalog https://github.com/terawarehouse/terawarehouse-react As...


Slack Discussion

Repositories
As in all application development, proper error handling must be in place to complete the system. In this case, we will present an entity not found exception during a get operation.

[open CategoryController class, oneCode method] - Code available below

In this case, we are throwing a CategoryNotFoundException if findByCode returns null. A CategoryNotFoundException extends an AbstractResourceNotFoundException with a constructor that formats the error message. This will serve as the parent class that can be extended for other entities.

Now, let’s go to the error handler. We can implement several ways, but for this exercise, we will use 2.

The first one is by using @RestControllerAdvice annotation.

[open CategoryNotFoundAdvice class] - Code available below

Here we have a method that accepts an exception and an @ExceptionHandler annotation that specifies the exception that it handles. We can also specify the response status via @ResponseStatus annotation. Or if you want you can return a ResponseEntity, and in that case, you can remove the @ResponseStatus annotation.

[open ApplicationExceptionResponseHandler class] - Code available below

The second approach is by extending the class ResponseEntityExceptionHandler, it lets you customize the Spring exception message. A well-known instance is when handling the validation errors as can be seen from the handleMethodArgumentNotValid method.

[open postman]

Run create test without code to see the formatted errors.

And that’s all for this lecture. I hope you have learned about error handling and how you can incorporate it to your application.

In the next video, we will discuss how you can version your API.

COMMENTS

mas template
Name

amazon,1,angular,8,bigdata,2,business,1,course-spring,27,courses,6,database,4,docker,3,java,50,kafka,1,keycloak,4,microservices,5,mysql,1,neworking,1,nosql,2,php,1,pinned,2,react,3,server management,7,shared drive,1,spring,7,synology,1,troubleshooting,2,web,1,wordpress,1,
ltr
item
toztech: RESTful API - Exception Handling
RESTful API - Exception Handling
https://i.ytimg.com/vi/8sPGjLgg7Wc/0.jpg
https://i.ytimg.com/vi/8sPGjLgg7Wc/0.jpg
toztech
https://toztech.blogspot.com/2019/08/restful-api-exception-handling.html
https://toztech.blogspot.com/
https://toztech.blogspot.com/
https://toztech.blogspot.com/2019/08/restful-api-exception-handling.html
true
2554149350007112447
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content