Solution for springboot springmvc to throw global exceptions

  • 2020-08-22 21:59:25
  • OfStack

springboot throws an exception. springboot comes with the springmvc framework, but I won't go into that.

springmvc series 1 exception resolution is described here. It's just combined with the use of springboot. It's ok that's effective and useful.

1. Define exception capture


package com.example.rest.error;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.validation.ConstraintViolationException;

/**
 *
 * @author ming  Define global exception handling 
 * @RestControllerAdvice  is @controlleradvice  with @ResponseBody  Combined annotations of 
 */
@RestControllerAdvice 
public class GlobalControllerExceptionHandler {

 @ExceptionHandler(value = { ConstraintViolationException.class })
 @ResponseStatus(HttpStatus.BAD_REQUEST)
 public ApiErrorResponse constraintViolationException(ConstraintViolationException ex) {
  return new ApiErrorResponse(500, 5001, ex.getMessage());
 }
 
 @ExceptionHandler(value = { IllegalArgumentException.class })
 @ResponseStatus(HttpStatus.BAD_REQUEST)
 public ApiErrorResponse IllegalArgumentException(IllegalArgumentException ex) {
  return new ApiErrorResponse(501, 5002, ex.getMessage());
 }

 @ExceptionHandler(value = { NoHandlerFoundException.class })
 @ResponseStatus(HttpStatus.NOT_FOUND)
 public ApiErrorResponse noHandlerFoundException(Exception ex) {
  return new ApiErrorResponse(404, 4041, ex.getMessage());
 }

 
 @ExceptionHandler(value = { Exception.class })
 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
 public ApiErrorResponse unknownException(Exception ex) {
  return new ApiErrorResponse(500, 5002, ex.getMessage());
 }
}

2. Define a return object


package com.example.rest.error;

/**
 * @author ming
 */
public class ApiErrorResponse {

 private int status;
 private int code;
 private String message;

 public ApiErrorResponse(int status, int code, String message) {
  this.status = status;
  this.code = code;
  this.message = message;
 }

 public int getStatus() {
  return status;
 }

 public int getCode() {
  return code;
 }

 public String getMessage() {
  return message;
 }

 @Override
 public String toString() {
  return "ApiErrorResponse{" +
    "status=" + status +
    ", code=" + code +
    ", message=" + message +
    '}';
 }
}

3. Define 1 startup Application


package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@SpringBootApplication
@EnableWebMvc 
public class SpringBootExceptionHandlingApplication {

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

4. The last test class


package com.example.rest.controller;

import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.ConstraintViolationException;
import java.util.Collections;

/**
 * @author ming
 */
@RestController
public class TestController {

 @GetMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
 public void test(Long id) {
  Assert.notNull(id,"id Can't be empty !");
  throw new ConstraintViolationException("error", Collections.emptySet());
 }
}

Note the configuration of the file application.properties


spring.mvc.throw-exception-if-no-handler-found=true 

ok, springboot to resolve the springmvc exception thrown can be resolved in this way.


Related articles: