资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

SpringBoot统一异常处理详解

代码结构

创新互联建站是一家集网站建设,十堰郧阳企业网站建设,十堰郧阳品牌网站建设,网站定制,十堰郧阳网站建设报价,网络营销,网络优化,十堰郧阳网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

SpringBoot 统一异常处理详解

配置pom文件

<?xml version="1.0" encoding="UTF-8"?>

 4.0.0

 bsea
 SpringBootException
 1.0-SNAPSHOT
 jar
 
  org.springframework.boot
  spring-boot-starter-parent
  1.5.14.RELEASE
   
 

 
  UTF-8
  UTF-8
  1.8
 

 
  
   org.springframework.boot
   spring-boot-starter-web
  

  
   org.springframework.boot
   spring-boot-starter-test
   test
  
 

 
  
   
    org.springframework.boot
    spring-boot-maven-plugin
   
  
 

启动类

package com.zz;

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,args);
 }
}

自定义异常类

package com.zz.exception;

public class UserNotExistException extends RuntimeException{
 private static final long serialVersionUID = -1574716826948451793L;

 private String id;

 public UserNotExistException(String id){
  super("user not exist");
  this.id = id;
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }
}

统一处理类

1. 在class上面加 @ControllerAdvice 表示全局异常处理类

2. 在方法的上面加@ExceptionHandler(UserNotExistException.class), 表示catch 到这个异常类型,并且在方法中处理, 不同的异常,可以通过不同的方法处理,每个方法上面都是通过这个注解括号里面的异常类来设置需要处理的异常类型。

package com.zz.handler;

import com.zz.exception.UserNotExistException;
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.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class ControllerExceptionHandler {
 //返回json的错误信息
 @ExceptionHandler(UserNotExistException.class)
 @ResponseBody
 @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
 public Map handleUserNotExistsException(UserNotExistException e) {
  Map map = new HashMap<>();
  map.put("id", e.getId());
  map.put("message", e.getMessage());
  return map;
 }
 //错误以后,跳转到自己定义的错误页面
 @ExceptionHandler(ArithmeticException.class)
 public String handle1(ArithmeticException e) {
  System.out.println("handle1 500*到了**************");
  return "/500.html";
 }
}

controller类

package com.zz.controller;

import com.zz.exception.UserNotExistException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {

 @GetMapping("/{id:\\d+}")
 public void get(@PathVariable String id) {

  throw new UserNotExistException(id);
 }
 @GetMapping("error2")
 public void get2() {
  int a=1/0;
 }
}

#运行结果

SpringBoot 统一异常处理详解

SpringBoot 统一异常处理详解

代码地址 github: 添加链接描述

以上所述是小编给大家介绍的SpringBoot统一异常处理详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!


网页题目:SpringBoot统一异常处理详解
地址分享:http://cdkjz.cn/article/pjccis.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220