欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

springboot springmvc抛出全局异常的解决方法

时间:2021-02-12 08:49:24|栏目:JAVA代码|点击:

springboot中抛出异常,springboot自带的是springmvc框架,这个就不多说了。

springmvc统一异常解决方法这里要说明的是。只是结合了springboot的使用而已。直接上代码,有效有用的才是ok。

1.定义异常捕获

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 定义全局异常处理
 * @RestControllerAdvice 是@controlleradvice 与@ResponseBody 的组合注解
 */
@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.定义一个返回对象

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.定义一个启动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.最后一个测试类

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不能为空!");
  throw new ConstraintViolationException("error", Collections.emptySet());
 }
}

注意application.properties这个文件的配置

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

ok,springboot中解决springmvc异常抛出就可以这样解决了。

上一篇:Java使用File类遍历目录及文件实例代码

栏    目:JAVA代码

下一篇:浅析git server“丢失”commit问题

本文标题:springboot springmvc抛出全局异常的解决方法

本文地址:http://www.codeinn.net/misctech/61547.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有