springmvc 全局异常处理

public class LoginValidateException extends RuntimeException {
    private String message;

    @Override
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public LoginValidateException(String message) {
        super();
        this.message = message;
    }
}

package com.hanshow.wise.portal.eshop.aspect;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 

Title: LoginValidateAspect

*

Description: 校验securityKey

*

Copyright: Copyright (c) 2018

*

Company: www.hanshow.com

* * @author guolin * @version 1.0 * @date 2018-06-09 11:48 */ public class LoginValidateAspect { private static org.slf4j.Logger logger = LoggerFactory.getLogger(LoginValidateAspect.class); private final static ObjectMapper mapper = new ObjectMapper(); /** * * @param request * @param jsonObject * @throws LoginValidateException */ public void before(HttpServletRequest request, JSONObject jsonObject) throws LoginValidateException { logger.info("请求参数:{}",jsonObject); String securityKey = RedisUtil.getSecurityKey(query.getMerchantId(), query.getMemberId()); if (securityKey != null && securityKey.equals(query.getSecurityKey())) { } else { logger.warn("securityKey校验失败"); Map returnMap = new HashMap<>();
          returnMap.put("code","fail");
          returnMap.put("message","securityKey校验失败");
throw new LoginValidateException(JSONObject.fromObject(returnMap).toString()); } } public void after(HttpServletRequest request, JSONObject jsonObject) { logger.info("[#RequestUrl##]:{},[#RequestJson#]:{}", request.getRequestURI(), jsonObject); } public void afterReturning(HttpServletRequest request, Object response, JSONObject jsonObject)throws JsonProcessingException { String reponseStr = mapper.writeValueAsString(response); logger.info("[#RequestUrl##]:{},[#RequestJson#]:{},[#reponseStr#]:{}", request.getRequestURI(), jsonObject, reponseStr); }}

package com.hanshow.wise.portal.eshop.resolver;

import com.hanshow.wise.portal.eshop.exception.LoginValidateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 

Title: CustomExceptionResolver

*

Description: 全局异常处理类

*

Copyright: Copyright (c) 2018

*

Company: www.hanshow.com

* @author guolin * @version 1.0 * @date 2018-06-09 14:10 */ public class CustomExceptionResolver implements HandlerExceptionResolver { private static Logger logger = LoggerFactory.getLogger(CustomExceptionResolver.class); @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) { ModelAndView modelAndView=new ModelAndView(); response.setStatus(HttpStatus.OK.value()); //设置状态码 response.setContentType(MediaType.APPLICATION_JSON_VALUE); //设置ContentType response.setCharacterEncoding("UTF-8"); //避免乱码 response.setHeader("Cache-Control", "no-cache, must-revalidate"); //如果该 异常类型是系统 自定义的异常,直接取出异常信息。 try{ if (exception instanceof LoginValidateException) { LoginValidateException loginValidateException = (LoginValidateException) exception; response.getWriter().write(loginValidateException.getMessage()); } else { response.getWriter().write(exception.getMessage()); } }catch (Exception e){ logger.error("全局异常处理失败",e); } return modelAndView; } }


	
		
			
			
			
			
			
			
			
			
			
			
			
			
		
	
	


你可能感兴趣的:(springmvc 全局异常处理)