All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.paas.aspect.PaaSHandlerExceptionAspect Maven / Gradle / Ivy

package com.paas.aspect;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.paas.service.HandlerResolveException;
import org.springframework.context.ApplicationContext;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName PaaSHandlerExceptionAspect
 * @Date 2020/12/9 11:04
 * @Auther wangyongyong
 * @Version 1.0
 * @Description TODO
 */
public class PaaSHandlerExceptionAspect extends DefaultHandlerExceptionResolver
{

    private ApplicationContext applicationContext;

    public PaaSHandlerExceptionAspect(ApplicationContext applicationContext)
    {
        this.applicationContext = applicationContext;
    }

    @Override
    @Nullable
    protected ModelAndView doResolveException(
            HttpServletRequest request, HttpServletResponse response, @Nullable Object handler, Exception ex)
    {
        try
        {
            Map handlerResolveExceptionsMap = applicationContext.getBeansOfType(HandlerResolveException.class);
            List handlerResolveExceptions = new ArrayList<>(handlerResolveExceptionsMap.values());
            if (!CollectionUtils.isEmpty(handlerResolveExceptions))
            {
                handlerResolveExceptions = handlerResolveExceptions.stream().sorted(Comparator.comparing(HandlerResolveException::getOrder)).collect(Collectors.toList());
                for (HandlerResolveException handlerResolveException : handlerResolveExceptions)
                {
                    if (handlerResolveException.supportException(handler).isInstance(ex))
                    {
                        Map map = handlerResolveException.doResolveException(ex);
                        if (null != map)
                        {
                            return new ModelAndView(new FastJsonJsonView(), map);
                        }
                    }
                }
            }
            return super.doResolveException(request, response, handler, ex);
        }
        catch (Exception handlerEx)
        {
            if (logger.isWarnEnabled())
            {
                logger.warn("Failure while trying to resolve exception [" + ex.getClass().getName() + "]", handlerEx);
            }
        }
        return null;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy