404 Exception not handled in Spring ControllerAdvice

半世苍凉 提交于 2020-05-25 20:20:46

问题


I have a simple Spring MVC application in which I want to handle all the unmapped urls using @ControllerAdvice. Here is the controller:

@ControllerAdvice
public class ExceptionHandlerController {
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(NoHandlerFoundException.class)
    public String handle404() {
        return "exceptions/404page";
    }
}

Still, every time get Whitelabel Error Page.

I tried using RuntimeException.class, HttpStatus.BAD_REQUEST and extending the class with NoHandlerFoundException but no use.

Any suggestions?


回答1:


To make it work, you need to set throwExceptionIfNoHandlerFound property on DispecherServlet. You can do that with:

spring.mvc.throwExceptionIfNoHandlerFound=true

in application.properties file, otherwise the requests will always be forwarded to the default servlet and NoHandlerFoundException would ever be thrown.

The problem is, even with this configuration, it doesn't work. From the documentation:

Note that if org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler is used, then requests will always be forwarded to the default servlet and NoHandlerFoundException would never be thrown in that case.

Because Spring Boot uses by default the org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler you'll have to override this using your own WebMvcConfigurer:

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        // Do nothing instead of configurer.enable();
    }
} 

Of course, the above class might be more complicated in your case.




回答2:


Another way to do so is ErrorController

@Controller
public class MyErrorController implements ErrorController {

    @GetMapping("/error")
    public ModelAndView errorHandler(HttpServletRequest req) {
        // Get status code to determine which view should be returned
        Object statusCode = req.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
        // In this case, status code will be shown in a view
        ModelAndView mav = new ModelAndView("error_default");
        mav.addObject("code", statusCode.toString());
        return mav;
    }

    public String getErrorPath() {
        return "/error";
    }
}


来源:https://stackoverflow.com/questions/54116245/404-exception-not-handled-in-spring-controlleradvice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!