简体   繁体   中英

Cannot start simple Spring Boot project

Using NetBeans 8.1.

I have this simple java Spring Boot project without any beans or persistency. Two java classes in the same package.

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

@Controller
public class WebController {
    @RequestMapping("/")
    public String index(){
        return "index";
    }
}

And a "index.html" file under src/main/resources->templates

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <h1>Test</h1>
    </body>
</html>

When i run it i have no errors but when i go to http://localhost:8080/ I have the Whitelabel Error Page :

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Feb 09 01:53:26 CET 2017
There was an unexpected error (type=Not Found, status=404).
No message available

First, whereas index.html is a static resource, it belongs under src/main/resources/static , not src/main/resources/templates .

Second, you don't need a request mapping for static resources. They just work.

So, move index.html , delete WebController , and it will work.

You place your html in a wrong directory. It is reason why you get this error.

In Spring Boot project, the default directory store html file to be src/main/webapp. However, it's hard show it with you here. I found this from the tutorial Spring Boot Maven Example Hello World with JSP

If you create Spring Boot project with Thymeleaf, the default directory is exactly src/main/resources/templates. You can refer to the post Spring Boot Thymeleaf Hello World Example

I just build project with both of cases, it works normally. Hope this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM