O'Reilly logo

Spring MVC Beginner’s Guide by Amuthan G

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – adding an exception handler

We must show a nice error message that says that no products were found with the given product ID. Let's do that with the help of @ExceptionHandler:

  1. Create a class called ProductNotFoundException under the com.packt.webstore.exception package in the source folder src/main/java. Now, add the following code to it:
    package com.packt.webstore.exception;
    
    public class ProductNotFoundException extends RuntimeException{
    
      private static final long serialVersionUID =-694354952032299587L;
    
      private String productId;
    
      public ProductNotFoundException(String productId) {
        this.productId = productId;
      }
    
      public String getProductId() {
        return productId;
      }
    
    }
  2. Now, open our InMemoryProductRepository class and modify the getProductById ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required