x Java Java 8 JUnit JSON
  • XML
  • JDBC Spring Boot Microservices React Contact Us

    Spring Boot @Controlleradvice Exception handler example

    By default Spring Boot shows a default or custom error page in case any Exception occurs. Sometimes instead of displaying an error page, you can return ResponseEntity with the appropriate HTTP status code and exception details. This can be achieved with the help of @ExceptionHandler and @ControllerAdvice annotations.

    You can also just use @ExceptionHandler for a particular endpoint or you can reuse the same @ExceptionHandler for whole application by moving the code to a seperate class and marking the class with @ControllerAdvice annotation.

    Step 1) Add below dependencies in pom.xml

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>        
    </dependencies>        

    Step 2) Create User, UserController, UserService, UserApplication classes

    package com.example.demo;
    
    public class User {
    
        private String id;
        private String firstName;
        private String lastName;
    
        // removed getter, setters, constructor
    
        @Override
        public String toString() {
            return "User [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
        }
    
    }      
    package com.example.demo;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/users")
    public class UserController {
    
        @Autowired
        private UserService userService;
        private static Logger logger = LoggerFactory.getLogger(UserController.class);
    
        @GetMapping("/{id}")
        public User get(@PathVariable("id") String id) throws Exception {
            logger.info("getting user");
            return userService.get(id);
        }
    
    }  

    package com.example.demo;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserService {
    
        private static Logger logger = LoggerFactory.getLogger(UserService.class);
        private Map<String, User> users = new HashMap<>();
    
        public User get(String id) {
            logger.info("getting user");
            if (!users.containsKey(id)) {
                throw new UserNotFoundException();
            }
            return users.get(id);
        }
    
        public void create(User user) {
            users.put(user.getId(), user);
        }
    } 

    package com.example.demo;
    
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    
    @SpringBootApplication
    public class UserApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(UserApplication.class, args);
        }
        
        @Bean
        public ApplicationRunner userInitializer(UserService userService) {
            return args -> {
                userService.create(new User("tom", "Tom", "Cruise"));
                userService.create(new User("harry", "Harry", "Potter"));
                userService.create(new User("bond", "James", "Bond"));
            };
        }
    }     

    Step 3) Create UserNotFoundException and UserExceptionHandler classes

    UserNotFoundException is our custom exception class which is a Runtime exception and is thrown when a user is not found. UserExceptionHandler is the exception handler class which handles UserNotFoundException thrown by the application and is annotated with @ControllerAdvice which means it will handle exceptions thrown from anywhere in the application.

    package com.example.demo;
    
    public class UserNotFoundException extends RuntimeException {
    
        private static final long serialVersionUID = 1L;
    
    }  

    package com.example.demo;
    
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.bind.annotation.ExceptionHandler;
    
    @ControllerAdvice
    public class UserExceptionHandler {
    
        @ExceptionHandler(UserNotFoundException.class)
        public ResponseEntity<Object> userNotFoundException() {
            return new ResponseEntity<>("User not found", HttpStatus.NOT_FOUND);
        }
    
    }   

    Step 4) Running UserApplication

    Run the UserApplication and open any browser and launch

    http://localhost:8080/users/ironman

    You will see below message displayed on the browser.

    User not found



    Using ExceptionHandler locally

    Note that if you don't want to create a global exception handler and want to use the exception handler only for a particular controller or endpoint, then you do not need to use a seperate UserExceptionHandler class and no need to annotate with @ControllerAdvice, instead you can add the logic in the particular controller itself as shown below.

    @Autowired
    private UserService userService;
    private static Logger logger = LoggerFactory.getLogger(UserController.class);
    
    @GetMapping("/{id}")
    public User get(@PathVariable("id") String id) throws Exception {
        logger.info("getting user");
        return userService.get(id);
    }
        
    @ExceptionHandler(UserNotFoundException.class)
    public ResponseEntity<Object> userNotFoundException() {
        return new ResponseEntity<>("User not found", HttpStatus.NOT_FOUND);
    }





    Comments

    Leave a Reply

    Your email address will not be published. Required fields are marked *











    Share This