Book Image

Spring MVC Beginner's Guide

By : Amuthan Ganeshan
Book Image

Spring MVC Beginner's Guide

By: Amuthan Ganeshan

Overview of this book

Table of Contents (19 chapters)
Spring MVC Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – adding the response status exception


First, we will look at the @ResponseStatus annotation (org.springframework.web.bind.annotation.ResponseStatus). In Chapter 3, Control Your Store with Controllers, we created a request mapping method to display products by category under the URI template, http://localhost:8080/webstore/products/{category}. If no products were found under the given category, we would show an empty web page, which is not correct semantically. We should show an HTTP status error to indicate that no products exist under the given category. Let's see how we can do that with the help of the @ResponseStatus annotation:

  1. Create a class called NoProductsFoundUnderCategoryException under the com.packt.webstore.exception package in the source folder, src/main/java. Now add the following code into it:

    package com.packt.webstore.exception;
    
    import org.springframework.http.HttpStatus;
    import org.springframework.web.bind.annotation.ResponseStatus;
    
    @ResponseStatus(value...