Posts

Showing posts with the label Spring Web Annotations

Spring @ResponseStatus Annotation Example

Image
In this section we will learn about @ResponseStatus  Annotation.   The  @ResponseStatus  marks a method or exception class with the status code and reason message that should be returned. The status code is applied to the HTTP response when the handler method is invoked, or whenever the specified exception is thrown. It overrides status information set by other means, like ResponseEntity or redirect. Here is a sample code snippet: @ResponseStatus ( value = HttpStatus . NOT_FOUND ) public class UserNotFoundException extends RuntimeException { } The following example creates a Spring Boot web application which uses  @ResponseStatus  annotation . Project Directory Pom.xml <? xml version ="1.0" encoding ="UTF-8" ?> < project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.

Spring @ModelAttribute Annotation Example

Image
In this section we will learn about  @ModelAttribute  Annotation.   The  @ModelAttribute   annotation refers to the Model object in MVC. We can pass @ModelAttribute annotation to method arguments or we can even annotate a method as well.  Passing @ModelAttribute to method argument indicates that the value should bind to the method argument. public String processForm ( @ModelAttribute ( "user" ) User user ) { user. doStuff ( ) ; } Assume that we have a form that is backed by the User object, when the user submits the form, the values should bind to the method argument with the help of @ ModelAttribute annotation.  By annotating @ModelAttribute annotation to method defines the object, which will automatically be added to the Model , and later we can use the Model object inside the view template. @ModelAttribute ( "user" ) public User getUser ( ) { return new User ( ) ; } Here the above method will allow access to the User object in our View sinc

Spring @ControllerAdvice Annotation Example

Image
In this section we will learn about  @ControllerAdvice Annotation.   The  @ControllerAdvice  is a specialization of the @Component annotation which sanctions to handle exceptions across the whole application in one global handling component. It can be viewed as an interceptor of exceptions thrown by methods annotated with @RequestMapping and kindred.  It declares @ExceptionHandler , @InitBinder , or @ModelAttribute methods to be shared across multiple @Controller classes. ResponseEntityExceptionHandler is a convenient base class for @ControllerAdvice classes that wish to provide centralized exception handling across all @RequestMapping methods through @ExceptionHandler methods. It provides an methods for handling internal Spring MVC exceptions. It returns a ResponseEntity in contrast to DefaultHandlerExceptionResolver which returns a ModelAndView . The following example creates a Spring Boot web application which uses  @ControllerAdvice .  Project Directory Pom.xml <? xml ve