Spring boot cannot find index.html under webapp folder
Solution 1
Spring Boot docs also says:
Do not use the src/main/webapp directory if your application will be packaged as a jar. Although this directory is a common standard, it will only work with war packaging and it will be silently ignored by most build tools if you generate a jar.
Spring Boot is very opinionated and works best when you do not try to resist defaults. I don't see any reason having your files placed in /src/main/webapp
. Just use /src/main/resources/static
for your front-end assets. That is most common place.
It will serve these static files from root URI automatically, without need to create any root level Controller
. In fact your IndexController
would prevent static front-end files to be served from root URI. There is no need to create Controller
for static files at all.
Also view resolver is not needed for your app. Your app is just REST API consumed by single page angular application. So your HTML templating is on client. View resolvers are needed if you are doing server side HTML templating (e.g. with Thymeleaf or JSP). So remove that piece also.
Solution 2
@RestController
public class IndexController {
@RequestMapping("/")
public String index(){
System.out.println("Looking in the index controller.........");
return "index";
}
}
Problem is here, you are using @RestController, so in this case, if you write "return 'index';" spring boot covers it as just string answer. You need use @Controller annotation instead.
Mike3355
Updated on September 10, 2020Comments
-
Mike3355 over 3 years
I read the following documentation from spring.io and it said
By default Spring Boot will serve static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath
however when I put my index.html file under/resources
the stringindex
is just rendered. Currentlyindex.html
is under webapp and I am using AngularJS.MvcConfiguration
@Configuration public class MvcConfig { @Bean InternalResourceViewResolver viewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/webapp/"); resolver.setSuffix(".html"); return resolver; } }
Restful Service for index page
@RestController public class IndexController { @RequestMapping("/") public String index(){ System.out.println("Looking in the index controller........."); return "index"; } }
ON my IDE console I do see
Looking in the index controller......
printed from IndexController and under network in the Chrome development tools I only seelocalhost 200
.index.html
<body> <!--[if lt IE 7]> <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> <![endif]--> <div ng-view></div> <div>Angular seed app: v<span app-version></span></div>
-
Mike3355 about 8 yearsYes it said do not put the index page in webapps IF you are putting it in a jar. I made a dummy index.html file put it in
/src/main/resources/static
and the stringindex
was still rendered. I do need a RestAPI because I am retrieving information from the@Service
. -
luboskrnac about 8 yearsbecause your IndexController is mapped to "/". When you want to serve static HTML files from your root resource, you shouldn't have any Controller mapped to "/".
-
Osama Al-Banna about 7 yearsI'm really confused actually when I create a new springboot project I don't have this folder webapp I only have static....where I should place my JSP files ?
-
Arya over 6 years@luboskrnac would the static folder also be the right place to put dynamically generated web pages?
-
luboskrnac over 6 yearsCan you elaborate more on "dynamically generated web pages"?