Advanced @ControllerAdvice in Spring Boot: Enhancing Exception Handling

Advanced @ControllerAdvice in Spring Boot: Enhancing Exception Handling


@ControllerAdvice is a powerful Spring annotation that centralizes exception handling across multiple controllers. In this post, we’ll explore advanced techniques for using @ControllerAdvice to create a robust error handling system in your Spring Boot application.

Key Features of @ControllerAdvice

  • Global exception handling
  • Centralized error response management
  • Ability to target specific controllers or packages

Advanced Implementation

Let’s dive into an advanced implementation of @ControllerAdvice:

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<ErrorResponse> handleResourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
        ErrorResponse errorResponse = new ErrorResponse(
            HttpStatus.NOT_FOUND.value(),
            ex.getMessage(),
            request.getDescription(false)
        );
        return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
    }

    @ExceptionHandler(ValidationException.class)
    public 
continue

Happy Vietnam National Teachers Day (20th Nov)

Thời tôi đi học, thật sự ấn tượng nhất có lẽ là năm lớp 5, năm cuối tiểu học.

Nhà tôi thuộc diện vùng xa, khỉ ho cò gáy, cả ấp có một cái trường mẫu giáo nhỏ đến mức chỉ có một phòng, khi tôi lớn lến mới hiểu nó nhỏ cỡ nào. Một căn nhà nhỏ được xây bằng gạch và lợp mái tôn xi măng kiễu cũ, thời gian phủ lên nó một màu đen lẫn với rêu. Tôi tự hỏi không biết bao thế hệ đã đi qua “ngôi nhà” trường này. Ngôi nhà có một … continue

Build a smooth iOS app

Build a smooth iOS app

In terms of building an iOS application, we want to make it smooth (aka, Responsive) (User can scroll up, scroll down, touch the buttons, swipe left, right, without lagging), It is important to understand the multithreading concept in iOS and how could we intelligently manage the way we call each operation( or function) to maximize the performance of CPU when it runs our app. (without crashing)

To archive that, we will learn about how Apple prepared the APIs to support Developers to build a Responsive app.

Managing tasks (or operations) is hard, and Apple handles continue

Midnight 29/01/2017

Đêm mùng 2 Tết


Tôi đẩy cánh cửa bước ra khỏi căn phòng ngập khói thuốc và tiếng chửi thề, tay lục túi tìm chìa khoá xe.

“Nay chơi trễ vậy anh”

“Ừm”

“Để em đẩy cho”

Nó đẩy chiếc xe to xác tôi ra và quay một cái xoẹt thành thục, kể ra nó cũng đã làm cả trăm lần hơn rồi còn gì.

Tôi móc túi còn hai mươi ngàn, dúi cho nó mười ngàn.

“Nè, cà phê”

“Thôi..”

“Ừm”

Nữa đêm, trời mưa lất phất và tôi bắt đầu phóng về khu nhà trọ bên Tân bình, continue

Reduce Massive View Controller

Reduce Massive View Controller’s complexity by using UIContentContainer and ChildViewController.

When we build a new screen, usually we jump right into adding a new View Controller, and/or ViewModel and regarding Services. Then get started to add more SubViews and Logic handler for controls and events.

Later after few updates, the ViewController has been manipulated by adding new controls and events which after come out to become too complex and hard to manage.

  • Too many Buttons, Label, Textfield, Stackview. When we look into the Layout in Interface Builder, we don’t know which button is doing what, and what is its IBOutlet
continue

Regex is hard without practising

Character classes

. matches any character except line breaks. Equivalent to [^\n\r].
\w matches any word character (alphanumeric & underscore). Only matches low-ascii characters (no accented or non-roman characters). Equivalent to [A-Za-z0-9_]
\d matches any digit character (0-9). Equivalent to [0-9].
\s matches any whitespace character (spaces, tabs, line breaks).
[ABC] matches any character in the set.
[^ABC] matches any character is not in the set.
[A-Z] matches a character having a character code between the two specified characters inclusive.

Anchors

^ matches the beginning of the string, or the beginning of a line if the multiline flag (m) is
continue

Advice from an Old Programmer

“…

I’ve been programming for a very long time. So long that it’s incredibly boring to me. At the time that I wrote this book, I knew about 20 programming languages and could learn new ones in about a day to a week depending on how weird they were. Eventually though this just became boring and couldn’t hold my interest anymore. This doesn’t mean I think programming is boring, or that you will think it’s boring, only that I find it uninteresting at this point in my journey.

What I discovered after this journey of learning is that it’s not … continue

How to Write Great Emails That Actually Get Things Done

(via Inc.com ) How can you make sure your emails get read — and more importantly, help get things done?
Here are some ways to write emails that quickly get to the point, save time, avoid misunderstandings, and make it easy for recipients to find the important bits when they (inevitably) revisit certain emails in the future. 1. Use keywords in the subject line.
Say you tend to have 50 or more opened emails in your inbox. And you store opened emails in a variety of folders. And you save all your sent mail.
That’s a lot of email 
continue

Thanks Auntie!

Lại một mùa Deepavali nữa, tôi có cảm giác như mình ở đây lâu lắm rồi. Nhiều lúc quen thuộc với những gì diễn ra xung quanh đến nỗi nó đem lại một cảm giác vừa thân thuộc vừa xa cách. Bạn có thể sẽ hỏi tôi sao lại thấy xa cách? Cũng dể hiểu thôi vì dù gì mình vẫn là người ngoại quốc và cho dù những gì xung quanh đây quá tốt với tôi thì một ngày nào đó, vì một lí do khách quan tôi không thể ở lại nữa. Xa cách là cảm giác bạn … continue