Friday 13 September 2019

Spring boot: @ExceptionHandler Example


@ExceptionHandler annotation is used to map given exception to handler method.

Example

@ExceptionHandler(NumberFormatException.class)
public ResponseEntity<AppError> handleNumberFormatException(NumberFormatException ex) {
 AppError error = new AppError();
 error.setMessaage(ex.getMessage());

 Optional<AppError> err = Optional.of(error);

 ResponseEntity<AppError> errEntity = ResponseEntity.of(err);

 return errEntity;

}

Find the below working example.


App.java
package com.sample.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {

 public static void main(String[] args) {
  SpringApplication.run(App.class);
 }

}


ExceptionsConfig.java
package com.sample.app.config;

import java.util.Optional;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.sample.app.model.AppError;

@Configuration
public class ExceptionsConfig {

 @ExceptionHandler(NumberFormatException.class)
 public ResponseEntity<AppError> handleNumberFormatException(NumberFormatException ex) {
  AppError error = new AppError();
  error.setMessaage(ex.getMessage());

  Optional<AppError> err = Optional.of(error);

  ResponseEntity<AppError> errEntity = ResponseEntity.of(err);

  return errEntity;

 }

 @ExceptionHandler(RuntimeException.class)
 public ResponseEntity<AppError> handleRuntimeException(RuntimeException ex) {
  AppError error = new AppError();
  error.setMessaage(ex.getMessage());
  
  Optional<AppError> err = Optional.of(error);

  ResponseEntity<AppError> errEntity = ResponseEntity.of(err);

  return errEntity;

 }
}


HelloController.java
package com.sample.app.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

 @RequestMapping("/")
 public String home() {
  return "Welcome to spring boot developement";
 }
 
 @RequestMapping("/greetMe")
 public String greetMe() {
  throw new NumberFormatException("Input can't be converted to number");
 }
 
 @RequestMapping("/hello")
 public String sayHello() {
  throw new RuntimeException("Run time Exception Occured");
 }
 
}


AppError.java
package com.sample.app.model;

public class AppError {

 private int statusCode = 500;
 private String messaage;

 public int getStatusCode() {
  return statusCode;
 }

 public void setStatusCode(int statusCode) {
  this.statusCode = statusCode;
 }

 public String getMessaage() {
  return messaage;
 }

 public void setMessaage(String messaage) {
  this.messaage = messaage;
 }

}


pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>springbootMisc</groupId>
 <artifactId>springbootMisc</artifactId>
 <version>1</version>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.6.RELEASE</version>
 </parent>

 <packaging>jar</packaging>

 <dependencies>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
 </dependencies>

</project>


Total project structure looks like below.

Run App.java.


Open the url ‘http://localhost:8080/hello’ in browser, you can see below screen.

You can download complete working application from this link.



Previous                                                    Next                                                    Home

No comments:

Post a Comment