31

I'm starting an embedded tomcat via spring-boot and want to serve a static index.html page as part of a running application.

But the following does not work:

@SpringBootApplication public class HMyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } @RestController public class HomeContoller { @RequestMapping("/") public String index() { return "index"; } } src/main/resources/static/index.html 

Result: when I call localhost:8080, I just see the word "index", but not my html page. Why?

7
  • 17
    That's because @RestController is a meta-annotation for @Controller and ResponseBody, meaning it writes "index" to the response input stream. You should use @Controller instead so that "index" will be resolved as a view name. Commented Aug 7, 2015 at 11:20
  • Omg you're right, absolutely. I probably was confused as developing a @RestController side by side. Anyways I'm now getting the following exception using @Controller: javax.servlet.ServletException: Could not resolve view with name 'index' in servlet with name 'dispatcherServlet'. It neither works returning index.html instead. Commented Aug 7, 2015 at 11:26
  • I think the view name should be static/index.html Commented Aug 7, 2015 at 11:29
  • Neither static/index nor static/index.html works. Commented Aug 7, 2015 at 11:30
  • See my answer below, I solved it. Commented Aug 7, 2015 at 11:46

4 Answers 4

18

My fault: I had an additional class with @EnableWebMvc annotation. This somehow messed up the spring-boot autoconfiguration. I removed it and now it works returning index.html.

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

Comments

11

For me this worked, i am sure there is a better way ( like without .html ).

@RequestMapping("/") public String index() { return "index.html"; } 

3 Comments

if your use the JSP page (index.jsp) Then you can use return "index".
This works, however if I change the Controller to a specific mapping it says 404. For example, Controller mapping: @Controller @RequestMapping("/user)" and the Method mapping: @RequestMapping("/{id}"), then I get a 404 error. It only works for the request mapping "/".
If you need json data as a response you should use the mapping in a @RestController class. If you need a view then it get's complicated a bit. What do you want to do? And usually if you put the static resources under a resources/static folder structure it works automatically because of spring boot. Also if you use /{id} you should have in the method params a @PathVariable("id") so it works, not sure if you left that out on purpose.
1

You can use ModelAndView in order to serve static HTML content in spring boot.

@RequestMapping("/") public ModelAndView home() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("index"); return modelAndView; } 

application.properties:-

spring.mvc.view.suffix = .html

HTML File : - src/main/resources/static/index.html

Comments

0

Thats because of @RestController annotation, just removing this annotation works for me.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.