Spring MVC框架:第十七章:异常映射

本文详细介绍了SpringMVC中异常映射的机制及其局限性,尤其在异步请求处理上的挑战。通过自定义异常解析器CrowdfundingExceptionResolver,实现了对异步请求异常的兼容处理,确保了Ajax请求的正常响应。

异常映射
异常机制是Java程序中针对有可能发生的问题所提前作出的应急解决方案。在SpringMVC中可以通过异常映射的方式,将异常类型和某个视图名称对应起来,让用户不是看到异常信息,而是一个比较友好的界面。

局限性:同步请求需要一个新的页面时这样操作是没问题的,但是对于需要数据片段的异步请求来说,就会导致Ajax请求收到的响应无法解析。
解决方案:

在spring-mvc.xml

<!-- 配置异常映射机制 -->
<!-- 为了能够兼容异步请求的异常信息响应格式,所以使用自定义CrowdfundingExceptionResolver类 -->
<bean id="simpleMappingExceptionResolver" class="com.crowdfunding.component.resolver.CrowdfundingExceptionResolver">
   <property name="exceptionMappings">
       <props>
           <prop key="java.lang.Exception">error</prop>
       </props>
   </property>
</bean>

分布式架构中没有Spring-mvc.xml那我们应该怎么办呢?
可以使用注解,例如:

@ControllerAdvice
public class MyExceptionResolver{

	@ExceptionHandler(value=Exception.class)
	public String resolveException(Exception e, Model model){
		model.addAttribute("exception",e);
		return "error";
	}

}

重写异常解析器的doResolveException()方法
为什么要重写?
在这里插入图片描述

重写示例

public class CrowdfundingExceptionResolver extends SimpleMappingExceptionResolver {
    
    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
            Exception ex) {
        //判断当前请求是否为异步请求.如果 X-Requested-With 为 null,则为同步请求。X-Requested-With 为 XMLHttpRequest 则为 Ajax 请求。
        if ((request.getHeader("accept").contains("application/json") || (request.getHeader("X-Requested-With") != null && request.getHeader("X-Requested-With").contains("XMLHttpRequest")))) {
            try {
                //创建ResultVO对象用来封装Ajax响应结果
                ResultVO<String> resultVO = new ResultVO<>();
                resultVO.setResult(ResultVO.FAILED);
                resultVO.setData(ResultVO.NO_DATA);
                resultVO.setMessage(ex.getMessage());
                //将resultVO对象转换为JSON数据
                Gson gson = new Gson();
                String json = gson.toJson(resultVO);
                //设置响应消息头
                response.setContentType("application/json;charset=UTF-8");
                //将resultVO的JSON数据返回给浏览器
                response.getWriter().write(json);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
        //如果不满足if条件说明是同步请求,则调用父类的方法按照常规方式处理
        return super.doResolveException(request, response, handler, ex);
    }
}

Gson的依赖
pom.xml

<dependency>
	<groupId>com.google.code.gson</groupId>
	<artifactId>gson</artifactId>
</dependency>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java程序员廖志伟

赏我包辣条呗

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值