0

This is my Controller class

@Controller
public class PageController {

    @GetMapping(value="/")
    public String homePage() {
        return "index";
    }   
}

And I also have a RestController class

@RestController
public class MyRestController {

    @Autowired
    private AddPostService addPostService;

    @PostMapping("/addpost")
    public boolean processBlogPost(@RequestBody BlogPost blogPost)
    {
        blogPost.setCreatedDate(new java.util.Date());
        addPostService.insertBlogPost(blogPost);

        return true;
    }
}

I have included all the necessary packages in the @ComponentScan of the Spring Application class.

I tried placing the index.html page in both src/main/resources/static and src/main/resources/templates. But when I load localhost:8080 it shows Whitelabel error page.

While debugging, the control is actually reaching return "index"; , but the page is not displayed.

3
  • what template engine are you using?, please post the pom file here Commented Nov 25, 2018 at 5:32
  • 1
    Possible duplicate of How to serve .html files with Spring Commented Nov 25, 2018 at 5:33
  • 1
    The "whitelabel error" also means you're getting an informative message on the server console. Commented Nov 25, 2018 at 6:07

2 Answers 2

1

The default view resolver will look in folders called resources, static and public.

So put your index.html in /resources/resources/index.html or /resources/static/index.html or /resources/public/index.html

You also need to return the full path for the file and the extension

@Controller
public class PageController {
    @GetMapping(value="/")
    public String homePage() {
        return "/index.html";
    }   
}

This makes your html page publicly available (e.g. http://localhost:8080/index.html will serve up the page) If this isn't what you want then you'll need to look at defining a view resolver.

Sign up to request clarification or add additional context in comments.

Comments

1

One of the correct behavior is to register your view in configuration and keep it under src/main/resources/templates/index.html:

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.