Java Spring Boot“无可用消息”错误(状态= 404),

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38205761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:04:35  来源:igfitidea点击:

Spring Boot "No message available" error(status = 404),

javaspringspring-mvc

提问by Vlad Sereda

I'm using Spring Boot with embeded Tomcat. When it starts it logs into console:

我正在使用带有嵌入式 Tomcat 的 Spring Boot。当它启动时,它会登录到控制台:

s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/home]}" onto public java.lang.String com.vlad.pet.contactlist.webapp.controller.SampleController.helloWorld(org.springframework.ui.Model)

swsmmaRequestMappingHandlerMapping :将“{[/home]}”映射到公共 java.lang.String com.vlad.pet.contactlist.webapp.controller.SampleController.helloWorld(org.springframework.ui.Model)

So I guess the URL is mapped to the controller. But http://localhost:8090/homegives me an error 404.

所以我猜 URL 被映射到控制器。但是http://localhost:8090/home给了我一个错误 404。

There was an unexpected error (type=Not Found, status=404). No message available

出现意外错误(类型=未找到,状态=404)。没有可用的消息

Application.java

应用程序.java

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    public static void main(final String[] args) {
        SpringApplication.run(Application.class, args);
    }
    //overrides the configure() method to point to itself, so Spring can find the main configuration.
    @Override
    protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
    @Bean
    public ViewResolver getViewResolver() {
        final TilesViewResolver resolver = new TilesViewResolver();
        resolver.setViewClass(TilesView.class);
        return resolver;
    }
    @Bean
    public TilesConfigurer getTilesConfigurer() {
        final TilesConfigurer configurer = new TilesConfigurer();
        configurer.setDefinitions("WEB-INF/tiles/tiles.xml");
        configurer.setCheckRefresh(true);
        return configurer;
    }
}

SampleController.java

示例控制器.java

@Controller
public class SampleController {
    @RequestMapping (value = "/home")
    public String helloWorld(Model model) {
        model.addAttribute("pageTitle", "home");
        return "base";
    }
}

tiles.xml

瓷砖.xml

<tiles-definitions>
    <definition name="base" template="/WEB-INF/tiles/basic/basic-template.jsp">
        <put-attribute name="head" value="/WEB-INF/tiles/basic/head.jsp" />
    </definition>
</tiles-definitions>

My project structure

我的项目结构

回答by exe

You're returning 'home' from your controller method, but I don't see a home.jsp on your project structure.

您正在从控制器方法返回 'home',但我在您的项目结构中没有看到 home.jsp。

回答by Vlad Sereda

I solved this problem by enabling default servlet:

我通过启用默认 servlet 解决了这个问题:

@Configuration
@EnableWebMvc
public class Config extends WebMvcConfigurerAdapter {
    @Override
    public void configureDefaultServletHandling(
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

Source: Configure ViewResolver with Spring Boot and annotations gives No mapping found for HTTP request with URI error

资料来源:使用 Spring Boot 配置 ViewResolver 和注释给出了未找到带有 URI 错误的 HTTP 请求的映射

回答by zxholy

You can use @RestControllerinstead of @Controller.

您可以使用@RestController代替@Controller.

@RestController
public class SampleController {
... 

回答by Cshah

In my case, my eclipse project was showing an error. i had to do mvn install from eclipse, update maven dependencies and then start the server from eclipse.

就我而言,我的 eclipse 项目显示错误。我必须从 eclipse 执行 mvn install,更新 maven 依赖项,然后从 eclipse 启动服务器。