Spring Framework SimpleMappingExceptionResolver - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Spring Framework SimpleMappingExceptionResolver class.

This code example shows how to use the following methods:setDefaultErrorView, setExceptionAttribute, setExceptionMappings, setWarnLogCategory
	 * @return The new resolver 
	 */ 
	@Bean(name = "simpleMappingExceptionResolver") 
	public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() {
		logger.info("Creating SimpleMappingExceptionResolver"); 
		SimpleMappingExceptionResolver r = new SimpleMappingExceptionResolver();
 
		Properties mappings = new Properties();
		mappings.setProperty("DatabaseException", "databaseException");
		mappings.setProperty("InvalidCreditCardException", "creditCardError");
 
		r.setExceptionMappings(mappings); // None by default
		r.setExceptionAttribute("ex"); // Default is "exception"
		r.setWarnLogCategory("demo1.ExceptionLogger"); // No default
 
		/* 
		 * Normally Spring MVC has no default error view and this class is the 
		 * only way to define one. A nice feature of Spring Boot is the ability 
		 * to provide a very basic default error view (otherwise the application 
		 * server typically returns a Java stack trace which is not acceptable 

6
CodeRank
This code example shows how to use the following methods:setExceptionMappings, setStatusCodes
        configurer.enable();
    } 
 
    @Bean 
    public SimpleMappingExceptionResolver exceptionResolver() {
        SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
 
        Properties exceptionMappings = new Properties();
 
        exceptionMappings.put("net.petrikainulainen.spring.testmvc.todo.exception.TodoNotFoundException", "error/404");
        exceptionMappings.put("java.lang.Exception", "error/error");
        exceptionMappings.put("java.lang.RuntimeException", "error/error");
 
        exceptionResolver.setExceptionMappings(exceptionMappings);
 
        Properties statusCodes = new Properties();
 
        statusCodes.put("error/404", "404");
        statusCodes.put("error/error", "500");
 

6
CodeRank
This code example shows how to use the following methods:setDefaultErrorView, setDefaultStatusCode, setExceptionMappings
public class ErrorHandling { 
	 
	SimpleMappingExceptionResolver getSimpleMappingExceptionResolver() {
		 
		SimpleMappingExceptionResolver result
		    = new SimpleMappingExceptionResolver();
		 
		// Setting customized exception mappings 
		Properties p = new Properties();
		p.put(CustomizedException1.class.getName(), "Errors/Exception1");
		result.setExceptionMappings(p);
		 
		// Un-mapped exceptions will be directed there 
		result.setDefaultErrorView("Errors/Default");
		 
		// Setting a default HTTP status code 
		result.setDefaultStatusCode(HttpStatus.BAD_REQUEST.value());
		 
		return result;
		 

6
CodeRank
This code example shows how to use the following methods:
 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
 
public class AdminMappingExceptionResolver extends SimpleMappingExceptionResolver { 
 
    private static final Log LOG = LogFactory.getLog(AdminMappingExceptionResolver.class); 
     
    protected boolean showDebugMessage = false; 
     
    @Override 
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,  
            Exception ex) { 
        if (BroadleafControllerUtility.isAjaxRequest(request)) { 
            // Set up some basic response attributes 
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 
            ModelAndView mav = new ModelAndView("utility/blcException"); 
             
            // Friendly message 
5
CodeRank
This code example shows how to use the following methods:determineViewName, getModelAndView
    } 
 
    private ModelAndView checkForAccessDenied(Exception ex, HttpServletRequest request) { 
        if (ex instanceof AccessDeniedException) { 
            ModelAndView modelAndView = null; 
            String viewName = determineViewName(ex, request); 
            if(viewName != null) { 
                modelAndView = getModelAndView(viewName, ex, request); 
            } 
            return modelAndView; 
        } 
 
        if (ex.getCause() != null && ex.getCause() instanceof Exception) { 
            return checkForAccessDenied((Exception) ex.getCause(), request); 
        } 
 
        return null; 
    } 
 
    private ModelAndView checkForPageExpiredException(Exception ex, HttpServletRequest request){ 
5
CodeRank
This code example shows how to use the following methods:setExceptionMappings, setStatusCodes
        configurer.enable();
    } 
 
    @Bean 
    public SimpleMappingExceptionResolver exceptionResolver() {
        SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
 
        Properties exceptionMappings = new Properties();
 
        exceptionMappings.put("java.lang.Exception", "error/error");
        exceptionMappings.put("java.lang.RuntimeException", "error/error");
 
        exceptionResolver.setExceptionMappings(exceptionMappings);
 
        Properties statusCodes = new Properties();
 
        statusCodes.put("error/404", "404");
        statusCodes.put("error/error", "500");
 
        exceptionResolver.setStatusCodes(statusCodes);

5
CodeRank
This code example shows how to use the following methods:
 
/** 
 * @author Roger 
 *  
 */ 
public class SampleExceptionHandler extends SimpleMappingExceptionResolver { 
 
	private static final Logger logger = LoggerFactory.getLogger(SampleExceptionHandler.class); 
 
	/** 
	 * Log the exception. 
	 *  
	 * @see org.springframework.web.servlet.handler.SimpleMappingExceptionResolver#doResolveException(javax.servlet.http.HttpServletRequest, 
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object, 
	 *      java.lang.Exception) 
	 */ 
	@Override 
	protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, 
			Exception ex) { 
		logger.error("A " + ex.getClass().getSimpleName() + " has occured in the application", ex); 
5
CodeRank
This code example shows how to use the following methods:
 * Extends the {@link SimpleMappingExceptionResolver} and adds the 
 * root-cause of the exception to the model. 
 *  
 * @author Felix Gnass [fgnass at neteye dot de] 
 */ 
public class AdvancedMappingExceptionResolver  
		extends SimpleMappingExceptionResolver { 
 
	private String rootCaseAttribute = "rootCause"; 
	 
	private Logger log = LoggerFactory.getLogger(AdvancedMappingExceptionResolver.class); 
	 
	public void setRootCaseAttribute(String rootCaseAttribute) { 
		this.rootCaseAttribute = rootCaseAttribute; 
	} 
 
	@Override 
	protected ModelAndView getModelAndView(String viewName, Exception ex) { 
		log.error("Unhandled exception", ex); 
		ModelAndView mv = super.getModelAndView(viewName, ex); 
5
CodeRank
This code example shows how to use the following methods:
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; 
 
import com.excilys.ebi.utils.spring.log.slf4j.InjectLogger; 
 
public class LoggingMappingExceptionResolver extends SimpleMappingExceptionResolver { 
 
	@InjectLogger 
	private Logger logger; 
 
	@Override 
	protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { 
 
		logger.error(ex.getMessage(), ex); 
		return super.doResolveException(request, response, handler, ex); 
	} 
} 
5
CodeRank
This code example shows how to use the following methods:
 * {@link SimpleMappingExceptionResolver} wasn't sufficient because it was logging all exceptions as warnings while 
 * some of them are expected and should be logged as INFO (such as 404 topic not found). 
 * 
 * @author Vitaliy Kravchenko 
 */ 
public class PrettyLogExceptionResolver extends SimpleMappingExceptionResolver { 
    /** Template message for logging AccessDeniedException */ 
    private static final String ACCESS_DENIED_MESSAGE = "Access was denied for user [%s] trying to %s %s"; 
    /** Constant for anonymous user */ 
    private static final String NOT_AUTHORIZED_USERNAME = "anonymousUser"; 
 
    /** 
     * {@inheritDoc} 
     */ 
    @Override 
    protected void logException(Exception ex, HttpServletRequest request) { 
        if (ex instanceof NotFoundException) { 
            logger.info(ex.getMessage()); 
        } else if (ex instanceof AccessDeniedException) { 
            String url = request.getRequestURL().toString(); 
5
CodeRank
See Code Examples for Spring Framework SimpleMappingExceptionResolver Methods: