springboot 全局异常拦截器,友好异常提示

1. 添加config 配置类

package org.fh.config;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

/**
 * 说明:错误异常拦截处理
 * 作者:FH Admin
 * from fhadmin.cn
 */
@Configuration
public class ExceptionConfiguration implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		ModelAndView mv = new ModelAndView(new MappingJackson2JsonView());	//返回json
		
		String exInfo = ex.toString().replaceAll("\n", "
"); boolean status = exInfo.contains("Subject does not have permission"); if(status){ exIn

你可能感兴趣的:(springboot)