Wednesday, 5 December 2018

Spring mvc: Exception handling: http response codes

This is continuation to my previous post. In my previous posts, I explained how to handle the exceptions, and redirect error scenarios to specific view pages. But there is one problem with that approach, even though, I am redirecting to error page, the http status code is still 200. Https status code 200 is for successful requests, it will give wrong impression to the user.


In case of error scenarios, we should send internal server error. We can do that by using @ResponseStatus annotation.

         @ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
         @ExceptionHandler(RuntimeException.class)
         public String handleRuntimeException() {
                  return "runtimeException";
         }


Find the below working application.

HelloWorldController.java
package com.sample.myApp.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloWorldController {

 
 @RequestMapping("/welcome")
 public String getHelloMessage() {
  boolean flag = true;

  if (flag) {
   throw new RuntimeException("Throwning blindly");
  }

  return "welcome";
 }

}

GlobalExceptionHandler.java
package com.sample.myApp.exceptionHandlers;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class GlobalExceptionHandler {

 @ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
 @ExceptionHandler(RuntimeException.class)
 public String handleRuntimeException() {
  return "runtimeException";
 }
}

Create runtimeException.jsp, welcome.jsp files under WEB-INF/jsp folder.


runtimeException.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <h1>Run time exception occured. Please contact the Administrator.
 </h1>
</body>
</html>

welcome.jsp
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World Spring Web MVC</title>
</head>
<body>
 <h1>
  Hello, Good Morning
 </h1>
</body>
</html>

Create web.xml, HelloWorld-servlet.xml files under WEB-INF folder.


web.xml
<web-app id="WebApp_ID" version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

 <display-name>Spring MVC Hello WorldApplication</display-name>

 <servlet>
  <servlet-name>HelloWorld</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>HelloWorld</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

</web-app>

HelloWorld-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

 <mvc:annotation-driven />

 <context:component-scan
  base-package="com.sample.myApp" />

 <bean
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>

</beans>

Create index.jsp file under webapp folder.


index.jsp
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<html>
<head>
<title>Welcome Page</title>
</head>

<body>
 <h2>Click on below button to get welcome page</h2>
 <form method="post" action="/springdemo/welcome"
  id="form1">

  <input type="submit" value="getWelcomeMessage"
   style="font-size: 18px;" />
 </form>

</body>
</html>

Project structure looks like below.

Run the application on server and hit the url http://localhost:8080/springdemo/welcome.

You can see the response status code 500 (Internal Server Error).


Previous                                                 Next                                                 Home

No comments:

Post a Comment