使用 Spring Boot 配置 ViewResolver 和注解给出了没有为 HTTP 请求找到映射,URI 错误 [英] Configure ViewResolver with Spring Boot and annotations gives No mapping found for HTTP request with URI error

查看:44
本文介绍了使用 Spring Boot 配置 ViewResolver 和注解给出了没有为 HTTP 请求找到映射,URI 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用最简单的视图解析器和 html 使用 gradle、spring boot 和 spring mvc 制作hello world"应用程序.

I'm trying to make "hello world" application with gradle, spring boot and spring mvc with the simplest view resolver and html.

我从 thymeleaf spring boot example 开始,我只是想删除 thymeleaf 以使用纯 html 和 InternalResourceViewResolver 制作更简单的 mvc 应用程序.我有一个我想要提供的 greeting.html,它位于 src/main/webapp/WEB-INF.当我运行应用程序时,我得到

I started from the thymeleaf spring boot example and I just wanted to remove thymeleaf to make a simpler mvc application using pure html and InternalResourceViewResolver. I have a single greeting.html I want to serve which is located at src/main/webapp/WEB-INF. When I run the app I get

No mapping found for HTTP request with URI [/WEB-INF/greeting.html] in DispatcherServlet with name 'dispatcherServlet'

这是一个常见错误,网上有很多答案,但似乎没有任何帮助.

This is a common error and there are a lot of answers on the web but nothing seems to help.

这是我的 Application.java

Here is my Application.java

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

这是我的 GreetingController.java

Here is my GreetingController.java

@Controller
public class GreetingController {
    @RequestMapping("/greeting")
    public String greeting() {
        return "greeting";
    }
}

这是我的 MvcConfiguration.java

Here is my MvcConfiguration.java

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".html");
        return resolver;
    }
}

我用 gradle bootRun

这是带有代码的存储库:https://github.com/driver-pete/spring-mvc-example

Here is the repo with the code: https://github.com/driver-pete/spring-mvc-example

这里还有一些线索:

  • Thymeleaf 视图解析工作正常
  • InternalResourceViewResolver 解析到正确的路径
  • WEB-INF 和 greeting.html 似乎存在于 war 文件中
  • 我没有 jsp 或 jstl,所以我不会像有些人建议的那样错过那些罐子

我的假设是调度程序 servlet 以某种方式被配置为在/* 而不是/like 这里 和任何地方.但是我没有 web.xml 所以这些建议在这里不适用.我看到了很多如何以编程方式配置调度程序 servlet 的示例,但我想将我的应用程序保持在最低限度,并且我怀疑 Spring Boot 应该可以对其进行配置,因为它可以与 thymeleaf 一起正常工作.

My hypothesis is that dispatcher servlet somehow get configured to serve on /* instead of / like here and everywhere. However I don't have web.xml so those advices do not apply here. I see a lot of examples how to configure dispatcher servlet programmatically but I want to keep my app at minimum and I suspect that spring boot is supposed to configure it ok since it works fine with thymeleaf.

推荐答案

您只需要启用默认的 servlet,这可以通过将以下内容添加到您的 MvcConfiguration 中来完成:

You only need to enable the default servlet, this is done by adding the following to your MvcConfiguration:

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
    @Bean
    public ViewResolver getViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".html");
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }    
}

本质上发生的事情是 Spring 不知道如何在本机处理此类内容(可能是 jsp 说的),而这种配置是告诉它委托给容器的方式.

Essentially what is happening is Spring does not know how to handle the handling of such content natively(could be a jsp say), and to this configuration is the way to tell it to delegate it to the container.

这篇关于使用 Spring Boot 配置 ViewResolver 和注解给出了没有为 HTTP 请求找到映射,URI 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆