LittleProgrammer
LittleProgrammer

Reputation: 47

Why Spring Boot 4 "custom error page" fails 404?

I tried to implement an own error page handling.But my page doesnt show up. Controller:

@Controller
public class MyCustomErrorController implements ErrorController {

@RequestMapping(value = "/error", method = RequestMethod.GET)
public String handleError() {

    return "error";
}

@Override
public String getErrorPath() {

    return "/error";
}}

I did my own error.html file in src/main/resources/static/html. The html folder is created by myself. Where is the problem?

<dependencies>
        <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <!-- <scope>provided</scope> -->
        </dependency>
    </dependencies>

Upvotes: 1

Views: 256

Answers (1)

Mykhailo Moskura
Mykhailo Moskura

Reputation: 2211

You can open your html file from static content like

localhost:8080/yourpagename

By default, Spring boot serves index.html as the root resource when accessing the root URL of a web application.

(yourhtml).html should exist under any of these paths:

src/main/resources/META-INF/resources/home.html

src/main/resources/resources/home.html

src/main/resources/static/home.html

src/main/resources/public/home.html

In order to view your error.html static page you need to return “error.html” in controller

In order to define your own static resource locations you could use this property in application.properties or application.yml file :

spring.resources.static-locations=your own locations

Upvotes: 1

Related Questions