Spring MVC @ExceptionHandler的使用

原文:https://blog.csdn.net/lsm135/article/details/52625483

Spring MVC 中的@ExceptionHandler可以对web的服务器端运行错误, 做统一的处理,使得http status code 从原本的500改成200, 

并去执行用@ExceptionHandler注解的方法。  前提是实现了这个方法的Class被 那个访问的Controller 继承了。

首先定义这样一个统一异常处理类

ExceptionController.java
 

package com.foundjet.front.base.controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
 
import net.sf.json.JSONObject;
 
public abstract class ExceptionController {
	@ExceptionHandler
	public @ResponseBody String exceptionProcess(HttpServletRequest request, HttpServletResponse
			response, RuntimeException ex) {
		JSONObject json = new JSONObject();
		json.put("isError", true);
		json.put("msg", ex.getMessage());
		return json.toString();
	}
}

在需要进行统一处理的类中extends 这个class

package com.foundjet.front.client.controller;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.foundjet.back.customer.service.CustomerService;
import com.foundjet.front.base.controller.ExceptionController;
import com.foundjet.front.base.controller.LoginAuth;
 
import net.sf.json.JSONObject;
 
@RequestMapping("/auth/customer")
@Controller
@LoginAuth
public class CustomerController extends ExceptionController {
	@Resource
	private CustomerService customerService;
	
	/**
	 * 
	 * @param page 第几页,从1开始
	 * @param rows 每页多少记录的行数
	 * @return JSON String
	 */
	@RequestMapping("/querybypage")
	@ResponseBody
	public String QueryByPage(int page, int rows) {
		//当为缺省值的时候进行赋值
        int currentpage = ( page == 0) ? 1: page;	//第几页
        int pageSize = (rows == 0) ? 5: rows;	//每页显示数量
        int index = (currentpage - 1) * pageSize;	//从第几条开始显示
        JSONObject map = customerService.queryByPage(index, pageSize);
        return map.toString();
	}
	
	@RequestMapping("/test")
	public @ResponseBody Object test() {
		JSONObject json = new JSONObject();
		json.put("status", "success");
		String str = null;
		str.toString();
		return json.toString();
	}
}

其中重要的一行是

public class CustomerController extends ExceptionController

这样CustomController里的方法被访问的时候, 如有异常, 就会被exceptionProces()处理

 

绕过web.xml中如下配置

web.xml

<!-- 错误跳转页面 -->
	<error-page>
		<!-- 路径不正确 -->
		<error-code>404</error-code>
		<location>/WEB-INF/errorpage/404.jsp</location>
	</error-page>
	<error-page>
		<!-- 没有访问权限,访问被禁止 -->
		<error-code>405</error-code>
		<location>/WEB-INF/errorpage/405.jsp</location>
	</error-page>
	<error-page>
		<!-- 内部错误 -->
		<error-code>500</error-code>
		<location>/WEB-INF/errorpage/500.jsp</location>
	</error-page>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值