Sunday, June 9, 2019

Creating Custom Error Page in Spring MVC

Its always a good Practice to create a custom Error Page which can be shown to client to handle errors such as "Page Not Found" or even "Internal Server Error"

We have created a controller which does the exactly same work.We have not created any views here which you can create in your views location with name "error.jsp" or "error.html" or as you have configured your view resolver.

Here is the file you can add to your Controllers.

ExceptionHandling.java
 package com.springimplant.mvc.controllers;  
 import javax.servlet.http.HttpServletRequest;  
 import javax.servlet.http.HttpServletResponse;  
 import org.springframework.core.annotation.AnnotationUtils;  
 import org.springframework.http.HttpStatus;  
 import org.springframework.stereotype.Controller;  
 import org.springframework.web.bind.annotation.ControllerAdvice;  
 import org.springframework.web.bind.annotation.ExceptionHandler;  
 import org.springframework.web.bind.annotation.RequestMapping;  
 import org.springframework.web.bind.annotation.ResponseStatus;  
 import org.springframework.web.servlet.ModelAndView;  
 @Controller  
 @ControllerAdvice  
 public class ExceptionHandling {  
      @ExceptionHandler(Exception.class)  
      public ModelAndView exceptionHandler(final HttpServletRequest request,final HttpServletResponse response,final Exception ex)  
      {  
           // If exception has a ResponseStatus annotation then use its response code  
           ResponseStatus responseStatusAnnotation = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class);  
           return buildModelAndViewErrorPage(request, response, ex,responseStatusAnnotation != null ? responseStatusAnnotation.value() : HttpStatus.INTERNAL_SERVER_ERROR);  
      }  
      @RequestMapping("*")  
      public ModelAndView fallbackHandler(final HttpServletRequest request, final HttpServletResponse response) throws Exception {  
           return buildModelAndViewErrorPage(request, response, null, HttpStatus.NOT_FOUND);  
      }  
      private ModelAndView buildModelAndViewErrorPage(final HttpServletRequest request,final HttpServletResponse response,final Exception ex,final HttpStatus httpStatus) {  
           response.setStatus(httpStatus.value());  
           ModelAndView mav=new ModelAndView("error");  
           if(ex!=null)  
           {  
                mav.addObject("title",ex);  
           }  
           mav.addObject("content",request.getRequestURL());  
           return mav;  
      }  
 }  

Thursday, June 6, 2019

Generating War file from Eclipse

  • In pom.xml from overview tab select packaging as war.
  • Next we need to add plugin to POM.xml
    • Right click on Pom.xml or Project =>select Maven=>select Add Plugin
    • Search for "maven-war-plugin" and add that plugin to pom.xml
  • Next if you don't have a web.xml file in your project to deploy i.e. it has been bootstrapped via java base classes add the following configuration
    •      <configuration>  
                 <failOnMissingWebXml>false</failOnMissingWebXml>  
           </configuration>
  • Right click on project and select Run as Maven Install.
Code to be added to Pom.xml
  <packaging>war</packaging>  
  <name>SpringMVCRestful</name>  
  <build>  
       <plugins>  
            <plugin>  
                 <groupId>org.apache.maven.plugins</groupId>  
                 <artifactId>maven-war-plugin</artifactId>  
                 <version>3.2.3</version>  
                  <configuration>  
                       <failOnMissingWebXml>false</failOnMissingWebXml>  
                  </configuration>  
            </plugin>  
       </plugins>  
  </build>  

*remove configuration from code if you are using web.xml for Bootstrapping

How do we Bootstrap via Java base classes not using web.xml

  • Create a class that extends "AbstractAnnotationConfigDispatcherServletInitializer".
    • This class will be instantiated when tomcat starts.
  • There are three abstract methods in this class
    • getRootConfigClasses()
      • Used to create Root Application Context such as Dispatcher Servlet
    • getServletConfigClasses()
      • Used to create Servlet Application Context.
    • getServletMappings()
      • Used to create URL Mappings for a servlet.
SimpleWebAppInitializer
 package com.springimplant.mvc.config;  
 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;  
 public class SimpleWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {  
      @Override  
      protected Class<?>[] getRootConfigClasses() {  
           return new Class[]{SimpleWebConfiguration.class};  
      }  
      @Override  
      protected Class<?>[] getServletConfigClasses() {  
           return null;  
      }  
      @Override  
      protected String[] getServletMappings() {  
           return new String[] {"/entry/*"};  
      }  
 }  

SimpleWebContext(Dispatcher Servlet Class)
 package com.springimplant.mvc.config;  
 import org.springframework.context.annotation.ComponentScan;  
 import org.springframework.context.annotation.Configuration;  
 import org.springframework.web.servlet.config.annotation.EnableWebMvc;  
 @Configuration  
 @EnableWebMvc  
 @ComponentScan(basePackages="com.springimplant.mvc.controllers")  
 public class SimpleWebConfiguration {  
 }  


Spring Boot

What is circular/cyclic dependency in spring boot? When two services are interdependent on each other, that is to start one service, we requ...