• SpringMVC之对JSON的支持以及全局异常处理


    目录

    一、SpringMVC之对JSON的支持

    1.JOSN的各种场景

    1.1.JSON解析

    1.2.导入pom依赖(Jackson)

    1.3.配置SpringMVC.xml (配置适配器)

    1.4.只使用注解@responseBody能够将任何的数据转成JSON对象

    1.4.1.后台代码

    ③.ClazzBiz

    测试

    前台

    1、返回list对象

    ​编辑 2、返回 List对象

     3、返回T对象

     4、返回Map对象

    ​ 5、返回混合对象

     二、SpringMVC的全局异常处理

    1.为什么要全局异常处理?

     2.异常处理思路

     3.SpringMVC异常分类

    4.   非全局异常问题处理

    5、异常处理

    5.1、使用SpringMVC自带的简单异常处理器SimpleMappingExceptionResolver

    5.2、通过HandlerExceptionResovler接口实现全局异常

    测试

    5.3、使用@ControllerAdvice+@ExceptionHandler实现全局异常 

    测试

     三、全局异常处理JSON返回

    测试


    一、SpringMVC之对JSON的支持

    1.JOSN的各种场景

    1. 返回List
    2. 返回List
    3. 返回T
    4. 返回Map
    5. 返回混合
    6. 返回JSON字符串

    1.1.JSON解析

    1. json的形式
    2. {}->json对象
    3. []->json数组
    4. {
    5. mag:"",
    6. code:200,
    7. data:[]
    8. }->json混合对象

    SpringMVC框架如何生产上述三种格式的数据
    1.导入pom依赖    Jackson        
    2.配置SpringMVC.xml 配置适配器
        作用是JSON数据转换的
    3.只使用注解@responseBody能够将任何的数据转成JSON对象

    1.2.导入pom依赖(Jackson)

    1. <dependency>
    2. <groupId>com.fasterxml.jackson.coregroupId>
    3. <artifactId>jackson-databindartifactId>
    4. <version>2.9.3version>
    5. dependency>
    6. <dependency>
    7. <groupId>com.fasterxml.jackson.coregroupId>
    8. <artifactId>jackson-coreartifactId>
    9. <version>2.9.3version>
    10. dependency>
    11. <dependency>
    12. <groupId>com.fasterxml.jackson.coregroupId>
    13. <artifactId>jackson-annotationsartifactId>
    14. <version>2.9.3version>
    15. dependency>

    1.3.配置SpringMVC.xml (配置适配器)

    1. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    2. <property name="messageConverters">
    3. <list>
    4. <ref bean="mappingJackson2HttpMessageConverter"/>
    5. list>
    6. property>
    7. bean>
    8. <bean id="mappingJackson2HttpMessageConverter"
    9. class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    10. <property name="supportedMediaTypes">
    11. <list>
    12. <value>text/html;charset=UTF-8value>
    13. <value>text/json;charset=UTF-8value>
    14. <value>application/json;charset=UTF-8value>
    15. list>
    16. property>
    17. bean>

    1.4.只使用注解@responseBody能够将任何的数据转成JSON对象

    在项目中如果全部返回json格式数据就用restController:responseBody+controller = restController

    在项目中如果返回json格式数据与跳转页面同时存在,就在返回json格式的方法上添加responseBody注解,在类上添加controller

    1.4.1.后台代码

    ①.ClazzMapper.xml

    1. "1.0" encoding="UTF-8" ?>
    2. mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    3. <mapper namespace="com.oyang.ssm.mapper.ClazzMapper" >
    4. <resultMap id="BaseResultMap" type="com.oyang.ssm.model.Clazz" >
    5. <constructor >
    6. <idArg column="cid" jdbcType="INTEGER" javaType="java.lang.Integer" />
    7. <arg column="cname" jdbcType="VARCHAR" javaType="java.lang.String" />
    8. <arg column="cteacher" jdbcType="VARCHAR" javaType="java.lang.String" />
    9. <arg column="pic" jdbcType="VARCHAR" javaType="java.lang.String" />
    10. constructor>
    11. resultMap>
    12. <sql id="Base_Column_List" >
    13. cid, cname, cteacher, pic
    14. sql>
    15. <select id="selectByPrimaryKey" resultType="com.oyang.ssm.model.Clazz" parameterType="com.oyang.ssm.model.Clazz" >
    16. select
    17. <include refid="Base_Column_List" />
    18. from t_struts_class
    19. where cid = #{cid,jdbcType=INTEGER}
    20. select>
    21. <select id="listPager" resultType="com.oyang.ssm.model.Clazz" parameterType="com.oyang.ssm.model.Clazz" >
    22. select
    23. <include refid="Base_Column_List" />
    24. from t_struts_class
    25. <where>
    26. <if test="cname!=null and cname!=''">
    27. and cname like concat('%',#{cname},'%')
    28. if>
    29. <if test="cid !=null and cid!=''">
    30. and cid = #{cid}
    31. if>
    32. where>
    33. select>
    34. <select id="listMapPager" resultType="java.util.Map" parameterType="com.oyang.ssm.model.Clazz" >
    35. select
    36. <include refid="Base_Column_List" />
    37. from t_struts_class
    38. <where>
    39. <if test="cname!=null and cname!=''">
    40. and cname like concat('%',#{cname},'%')
    41. if>
    42. <if test="cid !=null and cid!=''">
    43. and cid = #{cid}
    44. if>
    45. where>
    46. select>
    47. <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    48. delete from t_struts_class
    49. where cid = #{cid,jdbcType=INTEGER}
    50. delete>
    51. <insert id="insert" parameterType="com.oyang.ssm.model.Clazz" >
    52. insert into t_struts_class (cid, cname, cteacher,
    53. pic)
    54. values (#{cid,jdbcType=INTEGER}, #{cname,jdbcType=VARCHAR}, #{cteacher,jdbcType=VARCHAR},
    55. #{pic,jdbcType=VARCHAR})
    56. insert>
    57. <insert id="insertSelective" parameterType="com.oyang.ssm.model.Clazz" >
    58. insert into t_struts_class
    59. <trim prefix="(" suffix=")" suffixOverrides="," >
    60. <if test="cid != null" >
    61. cid,
    62. if>
    63. <if test="cname != null" >
    64. cname,
    65. if>
    66. <if test="cteacher != null" >
    67. cteacher,
    68. if>
    69. <if test="pic != null" >
    70. pic,
    71. if>
    72. trim>
    73. <trim prefix="values (" suffix=")" suffixOverrides="," >
    74. <if test="cid != null" >
    75. #{cid,jdbcType=INTEGER},
    76. if>
    77. <if test="cname != null" >
    78. #{cname,jdbcType=VARCHAR},
    79. if>
    80. <if test="cteacher != null" >
    81. #{cteacher,jdbcType=VARCHAR},
    82. if>
    83. <if test="pic != null" >
    84. #{pic,jdbcType=VARCHAR},
    85. if>
    86. trim>
    87. insert>
    88. <update id="updateByPrimaryKeySelective" parameterType="com.oyang.ssm.model.Clazz" >
    89. update t_struts_class
    90. <set >
    91. <if test="cname != null" >
    92. cname = #{cname,jdbcType=VARCHAR},
    93. if>
    94. <if test="cteacher != null" >
    95. cteacher = #{cteacher,jdbcType=VARCHAR},
    96. if>
    97. <if test="pic != null" >
    98. pic = #{pic,jdbcType=VARCHAR},
    99. if>
    100. set>
    101. where cid = #{cid,jdbcType=INTEGER}
    102. update>
    103. <update id="updateByPrimaryKey" parameterType="com.oyang.ssm.model.Clazz" >
    104. update t_struts_class
    105. set cname = #{cname,jdbcType=VARCHAR},
    106. cteacher = #{cteacher,jdbcType=VARCHAR},
    107. pic = #{pic,jdbcType=VARCHAR}
    108. where cid = #{cid,jdbcType=INTEGER}
    109. update>
    110. mapper>

    ②.ClazzMapper.java

    1. package com.oyang.ssm.mapper;
    2. import com.oyang.ssm.model.Clazz;
    3. import org.springframework.stereotype.Repository;
    4. import java.util.List;
    5. import java.util.Map;
    6. @Repository
    7. public interface ClazzMapper {
    8. int deleteByPrimaryKey(Integer cid);
    9. int insert(Clazz record);
    10. int insertSelective(Clazz record);
    11. Clazz selectByPrimaryKey(Integer cid);
    12. int updateByPrimaryKeySelective(Clazz record);
    13. int updateByPrimaryKey(Clazz record);
    14. List listPager(Clazz clazz);
    15. List listMapPager(Clazz clazz);
    16. }

    ③.ClazzBiz

    1. package com.oyang.ssm.biz;
    2. import com.oyang.ssm.model.Clazz;
    3. import com.oyang.ssm.util.PageBean;
    4. import java.util.List;
    5. import java.util.Map;
    6. public interface ClazzBiz {
    7. int deleteByPrimaryKey(Integer cid);
    8. int insert(Clazz record);
    9. int insertSelective(Clazz record);
    10. Clazz selectByPrimaryKey(Integer cid);
    11. int updateByPrimaryKeySelective(Clazz record);
    12. int updateByPrimaryKey(Clazz record);
    13. List listPager(Clazz clazz, PageBean pageBean);
    14. List listMapPager(Clazz clazz, PageBean pageBean);
    15. }

    ④.ClazzBizImpl

    1. package com.oyang.ssm.impl;
    2. import com.oyang.ssm.biz.ClazzBiz;
    3. import com.oyang.ssm.mapper.ClazzMapper;
    4. import com.oyang.ssm.model.Clazz;
    5. import com.oyang.ssm.util.PageBean;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.stereotype.Service;
    8. import java.util.List;
    9. import java.util.Map;
    10. /**
    11. * @author oyang
    12. * @site https://blog.csdn.net
    13. * @qq 1828190940
    14. * @create  2022-08-17 20:59
    15. */
    16. @Service
    17. public class ClazzBizImpl implements ClazzBiz {
    18. @Autowired
    19. private ClazzMapper clazzMapper;
    20. @Override
    21. public int deleteByPrimaryKey(Integer cid) {
    22. return clazzMapper.deleteByPrimaryKey(cid);
    23. }
    24. @Override
    25. public int insert(Clazz record) {
    26. return clazzMapper.insert(record);
    27. }
    28. @Override
    29. public int insertSelective(Clazz record) {
    30. return clazzMapper.insertSelective(record);
    31. }
    32. @Override
    33. public Clazz selectByPrimaryKey(Integer cid) {
    34. return clazzMapper.selectByPrimaryKey(cid);
    35. }
    36. @Override
    37. public int updateByPrimaryKeySelective(Clazz record) {
    38. return clazzMapper.updateByPrimaryKeySelective(record);
    39. }
    40. @Override
    41. public int updateByPrimaryKey(Clazz record) {
    42. return clazzMapper.updateByPrimaryKey(record);
    43. }
    44. @Override
    45. public List listPager(Clazz clazz, PageBean pageBean) {
    46. return clazzMapper.listPager(clazz);
    47. }
    48. @Override
    49. public List listMapPager(Clazz clazz, PageBean pageBean) {
    50. return clazzMapper.listMapPager(clazz);
    51. }
    52. }

    ⑤.JsonController

    1. package com.oyang.ssm.aspect;
    2. import com.oyang.ssm.biz.ClazzBiz;
    3. import com.oyang.ssm.model.Clazz;
    4. import com.oyang.ssm.util.PageBean;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.stereotype.Controller;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import org.springframework.web.bind.annotation.ResponseBody;
    9. import javax.servlet.http.HttpServletRequest;
    10. import java.util.HashMap;
    11. import java.util.List;
    12. import java.util.Map;
    13. /**
    14. * @author oyang
    15. * @site https://blog.csdn.net
    16. * @qq 1828190940
    17. * @create  2022-08-22 17:38
    18. */
    19. @Controller
    20. @RequestMapping("/clz/json")
    21. public class JsonController {
    22. @Autowired
    23. private ClazzBiz clazzBiz;
    24. @ResponseBody
    25. @RequestMapping("/clzEdit")
    26. public String clzEdit(){
    27. System.out.println("JsonController.clzEdit");
    28. return "clzEdit";
    29. }
    30. // list
    31. @ResponseBody
    32. @RequestMapping("/list")
    33. public List list(HttpServletRequest request,Clazz clazz){
    34. PageBean pageBean=new PageBean();
    35. pageBean.setRequest(request);
    36. // [{},{}]
    37. return this.clazzBiz.listPager(clazz,pageBean);
    38. }
    39. // list
    40. @ResponseBody
    41. @RequestMapping("/listMap")
    42. public List listMap(HttpServletRequest request, Clazz clazz){
    43. PageBean pageBean=new PageBean();
    44. pageBean.setRequest(request);
    45. // [{},{}]
    46. return this.clazzBiz.listMapPager(clazz,pageBean);
    47. }
    48. // Map
    49. @ResponseBody
    50. @RequestMapping("/map")
    51. public Map map(HttpServletRequest request, Clazz clazz){
    52. PageBean pageBean=new PageBean();
    53. pageBean.setRequest(request);
    54. // {}
    55. return this.clazzBiz.listMapPager(clazz,pageBean).get(0);
    56. }
    57. // T
    58. @ResponseBody
    59. @RequestMapping("/load")
    60. public Clazz load(HttpServletRequest request, Clazz clazz){
    61. PageBean pageBean=new PageBean();
    62. pageBean.setRequest(request);
    63. // {}
    64. return this.clazzBiz.listPager(clazz,pageBean).get(0);
    65. }
    66. // Map
    67. //{
    68. // mag:"",
    69. // code:200,
    70. // data:[],
    71. //PageBean:{}
    72. //}
    73. @ResponseBody
    74. @RequestMapping("/hunh")
    75. public Map hunh(HttpServletRequest request, Clazz clazz){
    76. PageBean pageBean=new PageBean();
    77. pageBean.setRequest(request);
    78. // {}
    79. List lst = this.clazzBiz.listPager(clazz, pageBean);
    80. Map map=new HashMap();
    81. map.put("data",lst);
    82. map.put("pagebean",pageBean);
    83. return map;
    84. // return this.clazzBiz.listPager(clazz,pageBean).get(0);
    85. }
    86. }

    测试

    前台

    ①. index.jsp

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: Administrator
    4. Date: 2022/8/23
    5. Time: 17:02
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>测试JSON数据返回title>
    12. head>
    13. <body>
    14. <a href="${pageContext.request.contextPath}/clz/json/list">返回list<T>对象a><hr>
    15. <a href="${pageContext.request.contextPath}/clz/json/listMap">返回list<Map>对象a><hr>
    16. <a href="${pageContext.request.contextPath}/clz/json/load?cid=1">返回T对象a><hr>
    17. <a href="${pageContext.request.contextPath}/clz/json/map?cid=1">返回Map对象a><hr>
    18. <a href="${pageContext.request.contextPath}/clz/json/hunh">返回混合对象a><hr>
    19. body>
    20. html>

    测试结果

    1、返回list对象

     2、返回 List对象

     3、返回T对象

     4、返回Map对象

    ​ 5、返回混合对象

     

     二、SpringMVC的全局异常处理

    1.为什么要全局异常处理?

    系统中异常包括:编译时异常运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。在开发中,不管是dao层、service层还是controller层,都有可能抛出异常,在springmvc中,能将所有类型的异常处理从各处理过程解耦出来,既保证了相关处理过程的功能较单一,也实现了异常信息的统一处理和维护

     2.异常处理思路

    系统的dao、service、controller出现异常都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理。springmvc提供全局异常处理器(一个系统只有一个异常处理器)进行统一异常处理。

     3.SpringMVC异常分类

    1)使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver; 
    2)实现Spring的异常处理接口HandlerExceptionResolver自定义自己的异常处理器;
    3)使用@ControllerAdvice + @ExceptionHandler

    4.   非全局异常问题处理

    在控制层Controller添加异常代码

    1. @ResponseBody
    2. @RequestMapping("/list")
    3. public List list(HttpServletRequest request,Clazz clazz){
    4. PageBean pageBean=new PageBean();
    5. pageBean.setRequest(request);
    6. if(true){
    7. throw new RuntimeException("查询班级信息异常,异常存在于JsonController.list...");
    8. }
    9. // [{},{}]
    10. return this.clazzBiz.listPager(clazz,pageBean);
    11. }

    在业务层implements添加异常代码

    1. @Override
    2. public List listMapPager(Clazz clazz, PageBean pageBean) {
    3. if(true){
    4. throw new RuntimeException("查询班级信息异常,异常存在于ClazzBizImpl.listMapPager... ");
    5. }
    6. return clazzMapper.listMapPager(clazz);
    7. }

    测试: 

    报错在Controller层 

     报错在Service

    5、异常处理

    5.1、使用SpringMVC自带的简单异常处理器SimpleMappingExceptionResolver

    编辑配置 Springmvc-Servlet.xml

    1. <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    2. <property name="defaultErrorView" value="error"/>
    3. <property name="exceptionAttribute" value="ex"/>
    4. <property name="exceptionMappings">
    5. <props>
    6. <prop key="java.lang.RuntimeException">errorprop>
    7. props>
    8. property>
    9. bean>

    新建一个jsp页面,收集错误信息

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: Administrator
    4. Date: 2022/8/23
    5. Time: 14:00
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>错误信息页面title>
    12. head>
    13. <body>
    14. ${ex}
    15. body>
    16. html>

    5.2、通过HandlerExceptionResovler接口实现全局异常

    先将上面Springmvc-Servlet.xml中关于异常处理的配置取消掉

    新建GlobalException类:

    1. package com.oyang.ssm.exception;
    2. /**
    3. * @author oyang
    4. * @site https://blog.csdn.net
    5. * @qq 1828190940
    6. * @create  2022-08-23 14:23
    7. */
    8. public class GlobalException extends RuntimeException{
    9. public GlobalException() {
    10. }
    11. public GlobalException(String message) {
    12. super(message);
    13. }
    14. public GlobalException(String message, Throwable cause) {
    15. super(message, cause);
    16. }
    17. public GlobalException(Throwable cause) {
    18. super(cause);
    19. }
    20. public GlobalException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
    21. super(message, cause, enableSuppression, writableStackTrace);
    22. }
    23. }

    全局异常处理器 GlobalHanderExceptionResovler 

    1. package com.oyang.ssm.exception;
    2. import org.springframework.stereotype.Component;
    3. import org.springframework.web.servlet.HandlerExceptionResolver;
    4. import org.springframework.web.servlet.ModelAndView;
    5. import javax.servlet.http.HttpServletRequest;
    6. import javax.servlet.http.HttpServletResponse;
    7. /**
    8. * @author oyang
    9. * @site https://blog.csdn.net
    10. * @qq 1828190940
    11. * @create  2022-08-23 14:30
    12. *
    13. * 处理全局异常的解析器
    14. */
    15. @Component
    16. public class GlobalHanderExceptionResovler implements HandlerExceptionResolver{
    17. /**
    18. *
    19. * @param httpServletRequest
    20. * @param httpServletResponse
    21. * @param o 目标对象
    22. * @param e 目标对象执行出现的异常对象
    23. * @return
    24. */
    25. @Override
    26. public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
    27. ModelAndView mv=new ModelAndView();
    28. mv.setViewName("error");
    29. if(e instanceof GlobalException){
    30. GlobalException exception=(GlobalException) e;
    31. mv.addObject("ex",exception.getMessage());
    32. mv.addObject("mag","全局异常,错误码502");
    33. }else if(e instanceof RuntimeException){
    34. RuntimeException exception=(RuntimeException) e;
    35. mv.addObject("ex",exception.getMessage());
    36. mv.addObject("mag","运行时异常,错误码602");
    37. }
    38. return mv;
    39. }
    40. }

    测试代码controller层的改变,改变load方法

    1. // T
    2. @ResponseBody
    3. @RequestMapping("/load")
    4. public Clazz load(HttpServletRequest request, Clazz clazz){
    5. PageBean pageBean=new PageBean();
    6. pageBean.setRequest(request);
    7. if(true){
    8. throw new GlobalException("系统繁忙·请参考Y001");
    9. }
    10. // {}
    11. return this.clazzBiz.listPager(clazz,pageBean).get(0);
    12. }

    提示错误界面

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: Administrator
    4. Date: 2022/8/23
    5. Time: 14:00
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>错误信息页面title>
    12. head>
    13. <body>
    14. ${mag}
    15. <hr>
    16. ${ex}
    17. body>
    18. html>

    测试

    运行时异常 

     全局异常

    5.3、使用@ControllerAdvice+@ExceptionHandler实现全局异常 

    为了不被第二种方式影响,我们将此注掉,注掉之后,以上的异常处理全部都会失效

     新建一个类 GlobalExceptionResolver

    1. package com.oyang.ssm.exception;
    2. import org.springframework.web.bind.annotation.ControllerAdvice;
    3. import org.springframework.web.bind.annotation.ExceptionHandler;
    4. import org.springframework.web.servlet.ModelAndView;
    5. /**
    6. * @author oyang
    7. * @site https://blog.csdn.net
    8. * @qq 1828190940
    9. * @create  2022-08-23 14:57
    10. */
    11. @ControllerAdvice
    12. public class GlobalExceptionResolver {
    13. @ExceptionHandler//异常处理器
    14. public ModelAndView handler(Exception e){
    15. ModelAndView mv=new ModelAndView();
    16. mv.setViewName("error");
    17. if(e instanceof GlobalException){
    18. GlobalException exception=(GlobalException) e;
    19. mv.addObject("ex",exception.getMessage());
    20. mv.addObject("mag","全局异常 GlobalExceptionResolver,错误码502");
    21. }else if(e instanceof RuntimeException){
    22. RuntimeException exception=(RuntimeException) e;
    23. mv.addObject("ex",exception.getMessage());
    24. mv.addObject("mag","运行时异常 GlobalExceptionResolver,错误码602");
    25. }
    26. return mv;
    27. }
    28. }

    测试

    运行时异常 

    全局异常 

     三、全局异常处理JSON返回

    在以后的开发中我们肯定是需要将错误码返回JSON格式,而不是跳转页面

    还是老样子,我们将注解注掉

    新建一个 GlobalExceptionResolver2 

    1. package com.oyang.ssm.exception;
    2. import org.springframework.web.bind.annotation.ExceptionHandler;
    3. import org.springframework.web.bind.annotation.RestControllerAdvice;
    4. import java.util.HashMap;
    5. import java.util.Map;
    6. /**
    7. * @author oyang
    8. * @site https://blog.csdn.net
    9. * @qq 1828190940
    10. * @create  2022-08-23 14:57
    11. *
    12. * 当出现异,统一向前端响应错误信息的JSON对象数据
    13. *
    14. */
    15. @RestControllerAdvice
    16. public class GlobalExceptionResolver2 {
    17. @ExceptionHandler//异常处理器
    18. public Map handler(Exception e){
    19. Map map=new HashMap();
    20. if(e instanceof GlobalException){
    21. GlobalException exception=(GlobalException) e;
    22. map.put("ex",exception.getMessage());
    23. map.put("mag","全局异常 GlobalExceptionResolver,错误码502");
    24. }else if(e instanceof RuntimeException){
    25. RuntimeException exception=(RuntimeException) e;
    26. map.put("ex",exception.getMessage());
    27. map.put("mag","运行时异常 GlobalExceptionResolver,错误码602");
    28. }
    29. return map;
    30. }
    31. }

    测试

    运行时异常

    全局异常

  • 相关阅读:
    transfromer-XL论文详解
    微信营销软件可靠吗?
    面经-常用框架
    NebulaGraph 的云产品交付实践
    计算机毕业设计Java桂林恒保健康防护有限公司官网(源码+系统+mysql数据库+Lw文档)
    LeetCode_位运算_中等_318.最大单词长度乘积
    【云原生 | 从零开始学Kubernetes】七、资源清单与Namespace
    一分钟解决 The server selected protocol version TLS10 is not accepted 问题
    从自媒体小白到优质KOL,你只差这些个人IP提效神器了!
    浅析多通道接收单元噪声系数的测试
  • 原文地址:https://blog.csdn.net/weixin_65211978/article/details/126471674