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

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

问题描述

我试图用最简单的视图解析器和html制作带有gradle,spring boot和spring mvc的hello world应用程序。 我从<一个href =https://spring.io/guides/gs/serving-web-content/ =nofollow noreferrer> thymeleaf spring boot example ,我只是想删除百里香以制作更简单的mvc应用程序使用纯html和InternalResourceViewResolver。我有一个greeting.html我想要服务,它位于src / main / webapp / WEB-INF。当我运行应用程序时,我得到

 未找到具有URI的HTTP请求的映射[/WEB-INF/greeting.html]名为'dispatcherServlet'的DispatcherServlet 

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



这是我的Application.java

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




$ b

这是我的GreetingController.java

  @Controller 
public class GreetingController {
@RequestMapping(/ greeting)
public String greeting() {
返回问候;


$ / code $ / pre

这里是我的MvcConfiguration.java

  @Configuration 
@EnableWebMvc
public class MvcConfiguration扩展WebMvcConfigurerAdapter {
@Bean
public ViewResolver getViewResolver (){
InternalResourceViewResolver解析器=新的InternalResourceViewResolver();
resolver.setPrefix(/ WEB-INF /);
resolver.setSuffix(。html);
返回解析器;


$ / code>

我用 gradle bootRun



以下是代码回购: https://github.com/driver-pete/spring-mvc-example



这里有一些更多线索:


  • Thymeleaf视图解析工作正常

  • InternalResourceViewResolver解析为正确路径
  • >
  • WEB-INF和greeting.html似乎存在于war文件中 我没有jsp或jstl,所以我不会错过那些jar文件可能会提示



我的假设是调度程序servlet以某种方式被配置为在/ *而不是/像这里和无处不在。但是我没有web.xml,所以这些建议不适用于此。我看到很多例子,如何以编程方式配置调度程序servlet,但我想保持我的应用程序的最低限度,我怀疑春季启动应该配置好,因为它适用于thymeleaf。

MvcConfiguration
中来完成的:

  @Configuration 
@EnableWebMvc
public class MvcConfiguration扩展WebMvcConfigurerAdapter {
@Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver解析器=新的InternalResourceViewResolver();
resolver.setPrefix(/ WEB-INF /);
resolver.setSuffix(。html);
返回解析器;


@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer配置器){
configurer.enable();




$ b $ p
$ b $ p基本上发生了什么是Spring不知道如何处理这些内容的本地处理(可以是jsp说),并且这种配置是告诉它将其委托给容器的方式。


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

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.

Here is my Application.java

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

Here is my GreetingController.java

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

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;
    }
}

I run it with gradle bootRun

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

Here are some more clues:

  • Thymeleaf view resolving works fine
  • InternalResourceViewResolver resolves to the right path
  • WEB-INF and greeting.html seems to be present in the war file
  • I do not have jsp or jstl so I do not miss those jars as some might suggest

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.

解决方案

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();
    }    
}

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,注释给出没有找到具有URI错误的HTTP请求的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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