For some hours I\'m trying to create a custom global error handler in Spring Boot/MVC. I\'ve read a lot of articles and nothing.
That is my error class:
Hope this will help: Create a class say: NoProductsFoundException that extends runtimexception.
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="No products found under this category")
public class NoProductsFoundException extends RuntimeException{
private static final long serialVersionUID =3935230281455340039L;
}
Then in your productcontroller:
@RequestMapping("/{category}")
public String getProductsByCategory(Model
model,@PathVariable("category") String category) {
List products = productService.getProductsByCategory(category);
if (products == null || products.isEmpty()) {
throw new NoProductsFoundException ();
}
model.addAttribute("products", products);
return "products";
}