【问题标题】:@EnableWebMvc annotation is affecting some mapping in spring boot@EnableWebMvc 注解正在影响 Spring Boot 中的一些映射
【发布时间】:2018-06-07 11:37:15
【问题描述】:

当我在我的项目中添加 @EnableWebMvc 注释时,它不会映射 webjars 和 favicon。

当我加载调用 webjars 的索引页面时收到此警告:

    No mapping found for HTTP request with URI [/webjars/bootstrap/4.0.0-beta.2/css/bootstrap.min.css] in DispatcherServlet with name 'dispatcherServlet'
    No mapping found for HTTP request with URI [/webjars/jquery/3.2.1/jquery.min.js] in DispatcherServlet with name 'dispatcherServlet'
    No mapping found for HTTP request with URI [/favicon.ico] in DispatcherServlet with name 'dispatcherServlet'

当我删除此注释时它可以正常工作,这是什么原因?我该如何解决这个问题?

我还需要这个注解来处理像这样的“NoHandlerFoundException”:

@ControllerAdvice
public class ControllerExceptionHandler {


    @ExceptionHandler(NoHandlerFoundException.class)
    public ModelAndView handlerNoHandlerFoundException() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("errorpage");
        modelAndView.addObject("errorTitle", "404 Not Found ");
        modelAndView.addObject("errorDescription", "The page you are looking for is not available now!!! ");
        System.out.println("ERROR :::::::::::::::::::::: no handler");
        return modelAndView;
    }

}

对于这种情况你有什么建议?

【问题讨论】:

  • 如果在您删除注释时它可以正常工作,您的问题是什么?只是没有注释。
  • @eis 我也需要这个注解来处理异常
  • 请在问题中添加您需要的异常处理详细信息。

标签: java spring spring-boot configuration mapping


【解决方案1】:

Per the Spring Boot documentation,使用 @EnableWebMvc 禁用 MVC 自动配置并允许您提供您想要的内容。在这种情况下,这意味着它会关闭默认资源和 WebJar 映射。

您可以使用其他方式来自定义您的配置,例如*Configurer,或者您可以使用@EnableWebMvc 准确指定您想要的配置。请参阅 Boot 的 WebMvcAutoConfiguration 以了解默认设置,然后复制您需要的部分。

【讨论】:

    猜你喜欢
    • 2018-06-21
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 2020-11-17
    • 2019-04-08
    • 2019-07-13
    • 2020-11-26
    • 2013-10-19
    相关资源
    最近更新 更多