springboot全局异常处理--记录

一、全局异常处理类

package com.example.demo.filter;

import org.apache.shiro.authz.AuthorizationException;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import redis.clients.jedis.exceptions.JedisConnectionException;

@ControllerAdvice
public class ExceptionFilter {
	@ExceptionHandler
	@ResponseBody
	public String ErrorHandler(Exception e) {
		return "未知异常!原因是" + e;
	}

	@ExceptionHandler
	@ResponseBody
	public String ErrorHandler(AuthorizationException e) {
		return "没有通过权限验证!";
	}

	@ExceptionHandler
	@ResponseBody
	public String ErrorHandler(JedisConnectionException e) {
		return "redis连接失败!";
	}

	@ExceptionHandler
	@ResponseBody
	public String ErrorHandler(MyBatisSystemException e) {
		return "数据库连接失败!";
	}
}

二、错误页面配置类

registerErrorPages.java

package com.example.demo.config;

import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

@Component
public class ErrorPageConfig implements ErrorPageRegistrar {

	@Override
	public void registerErrorPages(ErrorPageRegistry registry) {
		ErrorPage error400Page = new ErrorPage(HttpStatus.BAD_REQUEST, "/error/400");
		ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/error/401");
		ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404");
		ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500");
		registry.addErrorPages(error400Page, error401Page, error404Page, error500Page);
	}
}

ErrorPageController.java

package com.example.demo.config;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/error")
public class ErrorPageController {

	@RequestMapping("/{code}")
	public String toErrorPage(@PathVariable int code) {
		return "error/" + code;
	}

}

三、URL过滤器

package com.example.demo.filter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

@WebFilter(filterName = "MyFilter", urlPatterns = "/*")
public class UrlFilter implements Filter {
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
		try {
			HttpServletRequest httpRequest = (HttpServletRequest) request;
			// HttpServletResponse httpResponse = (HttpServletResponse) response;
			String url = httpRequest.getRequestURL().toString();
			String qryString = httpRequest.getQueryString();

			System.out.println(url + (qryString == null ? "" : "?" + qryString));

			chain.doFilter(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void destroy() {
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值