Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Spring Boot - Service Class Example for Displaying Response Codes and Custom Error Codes
Next article icon

Spring Boot - Service Class Example for Displaying Response Codes and Custom Error Codes

Last Updated : 25 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Sometimes the error or status messages in webpages would be different from the default error message thrown by Tomcat (or any other server). An important reason for this is websites want to look unique in their approach to their users. So, a customized page for displaying status codes is always better than the standard template. Spring Boot makes it easier in this case to handle several possibilities of wrong URLs entered by the user. To start with, Spring Boot is simple enough for basic configurations with Spring Initializr (https://start.spring.io/). The following steps need to be followed after going to the starter site:

  1. The site would already have default values for group, artifact, package name, etc which can be changed per the requirements.
  2. The latest stable version of Spring would have been selected in the starter by default but you can try the unstable SNAPSHOT and M3 versions if you prefer.
  3. There is also an option to add dependencies. For our tutorial we would be coding only RESTful web services, it would be sufficient to add Spring Web and Spring Boot DevTools dependency. Spring Boot DevTools provides features faster application restarts, LiveReload, and other configurations which ensures that the developer need not restart the application every time the code is changed.

After all the steps, the project can be downloaded as a zip file. After extracting the content in the zip files, it can be imported into the IDE by clicking:

  1. Import -> Maven -> Existing Maven Projects -> Next
  2. Click browse, go to the location where the project is downloaded, select the project folder and click select folder.

Now it's time to mention the output expectations for this tutorial. The expected output is demonstrated with screenshots. The service class is expected to show output only for GET requests, other methods should display a 405 error code.

 

If the parameter values do not match the specified possible values or no parameter is present, a page not found error page should be displayed.

 

A request to GET /format=text should return a simple return message displaying the current time and status code as OK using HashMap as the response data format.

 

A request to GET /format=page should display the webpage. This is subjective in each case and in this case we are only going to display a sample webpage available in the public domain.

 

The above requirements can be coded in Controller classes annotated with the @RestController annotation as demonstrated in the example given below.

Controller.java File

Java
package com.springboot.springbootdemo;  import java.lang.annotation.Repeatable; import java.net.URI; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.HashMap; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.server.MethodNotAllowedException;  @RestController public class Controller implements ErrorController {      @GetMapping("/format=page")     public ResponseEntity<Object> page()     {         return ResponseEntity.status(HttpStatus.FOUND)             .location(URI.create(                 "https://www.geeksforgeeks.org/resource-injection-in-java-ee/"))             .build();     }      @GetMapping("/format=text")     public HashMap<String, String> text()     {         HashMap<String, String> map             = new HashMap<String, String>();         SimpleDateFormat formatter = new SimpleDateFormat(             "yyyy-MM-dd'T'HH:mm:ssZ");         Date date = new Date(System.currentTimeMillis());         String formattedString = formatter.format(date);         map.put("currentTime", formattedString);         map.put("status", "OK");         return map;     }      @PostMapping("/**")     public HashMap<String, String> all_except_get()     {         HashMap<String, String> map             = new HashMap<String, String>();         map.put("status", "401");         return map;     }      @DeleteMapping("/**")     public HashMap<String, String> all_delete()     {         HashMap<String, String> map = all_except_get();         return map;     }      @PutMapping("/**")     public HashMap<String, String> all_put()     {         HashMap<String, String> map = all_except_get();         return map;     }      @PatchMapping("/**")     public HashMap<String, String> all_patch()     {         HashMap<String, String> map = all_except_get();         return map;     }      @RequestMapping("/error")     public ResponseEntity<Object> handleError()     {         return ResponseEntity.status(HttpStatus.FOUND)             .location(URI.create(                 "http://localhost:8002/LoginRegistration/404error.html"))             .build();     }      public String getErrorPath() { return "/error"; } } 

An explainer of the code given above:

  • The function ResponseEntity<Object> page() annotated with @GetMapping("/format=page") handles requests to GET /format=page by redirecting the control to the webpage "https://www.geeksforgeeks.org/resource-injection-in-java-ee/" with the command 'return ResponseEntity.status(HttpStatus.FOUND).location(URI.create("https://www.geeksforgeeks.org/resource-injection-in-java-ee/")).build();'
  • URI.create() creates a new URI object which parses the webpage link as URI.
  • The location() method searches for the location of the URI to which the control is to be redirected. HttpStatus.FOUND returns the response code which signifies whether the URL is reachable or not.
  • The build() method builds this entire stream of commands which is then processed by ResponseEntity.status. This entire response is then returned by the Java function after which the control is redirected to the given URL depending on the status code for the request.
  • The function ResponseEntity<Object> text annotated with @GetMapping("/format=text") handles requests to GET /format=text by declaring and returning the object of type HashMap<String, String>.
  • The object returns the HashMap with two key-value pairs. One key-value pair is the status code with the message "OK" and another key-value pair is the current date and time formatted as an ISO 8601 string, i.e, in the "yyyy-MM-dd'T'HH:mm'Z" format.
  • The above format can be achieved using the formatter command "SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");". The current date and time is stored in a variable using the command "Date date = new Date(System.currentTimeMillis());" which is then formatted into a string using the formatter "String formattedString=formatter.format(date);" which we declared just now.
  • The formatted string is then added to the map along with the status message. Now, the message to be displayed for the corresponding URL is returned as a map.
  • Since the service is expected to handle only GET requests and a 405 error is to be shown for other requests, ("/**") is used after each annotation mapping to receive all requests for a given type (POST, PUT, DELETE and PATCH) and throw the 405 error message.

For example,

Java
@PostMapping("/**") public HashMap<String, String> all_except_get() {     HashMap<String, String> map         = new HashMap<String, String>();     map.put("status", "405");     return map; } 

is the function to handle all POST requests. Similarly, the function given below handles all the DELETE requests:

Java
@PutMapping("/**") public HashMap<String, String> all_put() { HashMap<String, String> map = all_except_get();      return map; } 

As it is evident from the above code, a map with a status code need not be declared and returned each time. Since it has already been defined once, the function is invoked to get the map value into the variable which is then returned. The same procedure is followed for @DeleteMapping("/**") and @PatchMapping("/**") as well.

application.properties File

server.port=8075  server.error.whitelabel.enabled=false

Before coding the custom error handler methods, it is necessary to add the lines "server.error.whitelabel.enabled=false" in the application.properties file so that the default whitelabel error page can be overridden. 

Note: Spring Boot uses port 8080 by default to start Tomcat. If it is required to use a different port, the port number has to be mentioned in the application.properties file with the server.port=8075.

Now back to coding the error handler methods in the service class. The getErrorPath() method returns the path of the error page, which in this case returns the "/error" which is appended to the URL. Subsequently, there has to be a function to handle the error URL, which is given below:

Java
@RequestMapping("/error") public ResponseEntity<Object> handleError() {     return ResponseEntity.status(HttpStatus.FOUND)         .location(URI.create(             "http://localhost:8002/LoginRegistration/404error.html"))         .build(); } 

Similar to how it was demonstrated for the @GetMapping("/format=page"), the return statement transfers the control to the error page. Some functions that can be coded using the service class, including the custom error function, are demonstrated and explained in the examples given above.


Next Article
Spring Boot - Service Class Example for Displaying Response Codes and Custom Error Codes

A

anishnarayan_p
Improve
Article Tags :
  • Java
Practice Tags :
  • Java

Similar Reads

    Message Compression in Apache Kafka using Spring Boot
    Generally, producers send text-based data, such as JSON data. It is essential to apply compression to the producer in this situation. Producer messages are transmitted uncompressed by default. There are two types of Kafka compression. 1. Producer-Level Kafka Compression When compression is enabled o
    4 min read
    Spring Boot - Create and Configure Topics in Apache Kafka
    Topics are a special and essential component of Apache Kafka that are used to organize events or messages. In other words, Kafka Topics enable simple data transmission and reception across Kafka Servers by acting as Virtual Groups or Logs that store messages and events in a logical sequence. In this
    2 min read
    How to Test Spring Boot Project using ZeroCode?
    Zerocode automated testing framework for a REST API project concept is getting seen via this tutorial by taking a sample spring boot maven project. Let us see the dependencies for Zerocode : <dependency> <groupId>org.jsmart</groupId> <artifactId>zerocode-tdd</artifactId
    5 min read
    Validation in Spring Boot
    In this article, via a Gradle project, let us see how to validate a sample application and show the output in the browser. The application is prepared as of type Spring Boot and in this article let us see how to execute via the command line as well. Example Project Project Structure:   As this is th
    5 min read
    Spring Boot – Validation using Hibernate Validator
    Hibernate Validator provides a powerful and flexible way to validate data in Spring Boot applications. Validating user input is essential for building secure and reliable applications. Spring Boot makes this easy with Hibernate Validator, the reference implementation of JSR 380 (Bean Validation API)
    6 min read
    How to Connect MongoDB with Spring Boot?
    In recent times MongoDB has been the most used database in the software industry. It's easy to use and learn This database stands on top of document databases it provides the scalability and flexibility that you want with the querying and indexing that you need. In this, we will explain how we conne
    4 min read
    Spring Boot - File Handling
    Spring Boot is a popular, open-source spring-based framework used to develop robust web applications and microservices. As it is built on top of Spring Framework it not only has all the features of Spring but also includes certain special features such as auto-configuration, health checks, etc. whic
    5 min read
    Spring Boot MockMVC Testing with Example Project
    In a Spring Boot project, we have to test the web layer. For that, we can use MockMVC. In this tutorial, let us see how to do that by having a sample GeekEmployee bean and writing the business logic as well as the test cases for it. Example Project Project Structure:   This is a maven project. Let's
    5 min read
    Spring Boot Integration With MySQL as a Maven Project
    Spring Boot is trending and it is an extension of the spring framework but it reduces the huge configuration settings that need to be set in a spring framework. In terms of dependencies, it reduces a lot and minimized the dependency add-ons. It extends maximum support to all RDBMS databases like MyS
    4 min read
    Spring Boot Integration With MongoDB as a Maven Project
    MongoDB is a NoSQL database and it is getting used in software industries a lot because there is no strict schema like RDBMS that needs to be observed. It is a document-based model and less hassle in the structure of the collection. In this article let us see how it gets used with SpringBoot as a Ma
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences