nl.pinniq.web.controller.ExceptionController.java Source code

Java tutorial

Introduction

Here is the source code for nl.pinniq.web.controller.ExceptionController.java

Source

/*
Copyright 2015 pinniq
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package nl.pinniq.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

@ControllerAdvice
public class ExceptionController {

    private static final String DEFAULT_ERROR_VIEW = "/oops";

    @Autowired
    HttpServletRequest request;

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public ModelAndView handleNoHandlerFoundException(NoHandlerFoundException ex) {
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("exception", ex);
        m.put("errorcode", "404");
        m.put("url", request.getRequestURL().toString());
        ModelAndView mav = new ModelAndView(DEFAULT_ERROR_VIEW, "model", m);
        return mav;
    }

}