0

In my Springboot application even though the rest endpoint is being hit correctly as seen from log message I am getting the following 404 error.

I have a springboot application with the following main class:-

package com.springbootbasic; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootBasicApplication { public static void main(String[] args) { SpringApplication.run(SpringbootBasicApplication.class, args); } } 

Following is the controller class:-

package com.springbootbasic; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HomeController { Logger logger = LoggerFactory.getLogger(HomeController.class); @RequestMapping("/hello") public String home(Model model) { logger.info("Rest endpoint /hello is being hit correctly."); return "index.html"; } @RequestMapping("/home") @ResponseBody public String response() { logger.info("Rest endpoint /home is being hit correctly."); return "Very well done the rest end point is working fine."; } } 

The above two classes are in the same package and the HomeController is annotated with @Controller and not the @RestController.

And in the src/main/public source folder of eclipse I have created the following index.html file.

<!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Welcome from Home</title> </head> <body> <h2>Hello World!</h2> </body> </html> 

And in the browser when I am hitting the url http://localhost:8080/hello I am getting the following error :-

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

But the log message is :-

2019-08-28 20:22:38.953 INFO 20864 --- [nio-8080-exec-1] c.springbootbasic.HomeController : Rest endpoint /hello is being hit correctly. 2019-08-28 20:22:52.179 INFO 20864 --- [nio-8080-exec-3] c.springbootbasic.HomeController : Rest endpoint /home is being hit correctly. 2019-08-28 20:25:20.858 INFO 20864 --- [nio-8080-exec-6] c.springbootbasic.HomeController : Rest endpoint /hello is being hit correctly. 

How to fix this?

2 Answers 2

1

The problem is the name of your template. Your template's name is index and if you hit http://localhost:8080/ you can see yourpage. I have had the same problem and if you rename your index.html should be fine. I don't know exactly why this happens, it can be related with the spring-boot embedded server.

EDIT: From https://spring.io/guides/gs/serving-web-content/

The index.html resource is special because it is used as a "welcome page" if it exists, which means it will be served up as the root resource, i.e. at http://localhost:8080/

Hope this helps.

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

5 Comments

Even If I change the name of the html file, then also the problem remains. I had used earlier the RequestMapping("/") with the name of the html file to be index.html. But this does not seems to be working.
I just read you create the index.html in src/main/public but all the templates must be in src/main/resources/templates. Could you move your file? If this solve the problem i will edit my answer
By default, Springboot serves the static content from the src/main/public/ and src/main/static/ directories.
Do you use thymeleaf or other template renderer?
No here its just plain and simple static html page.
1

You should create Internal View Resolver and add it in your configure adapter

1 Comment

Is this required if we were to serve the content from the src/main/public or src/main/static folders from where the springboot serves the content by default. By default at least to me means that you dont need any separate configuration to get it done. Moreover please kindly show with code what are you suggesting me to do.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.