1

I have a Spring Boot app setup as a REST api. I now also want to be able to serve simple HTML pages to the client, without the use on any template engine like Thymeleaf. I want access to the HTML pages to fall under the same security constraints setup by Spring Security with the use of WebSecurityConfigurerAdapter, already present in my app.


What I've tried is having a Controller:

@Controller
public class HtmlPageController {
    @RequestMapping(value = "/some/path/test", method = RequestMethod.GET)
    public String getTestPage() {
        return "test.html";
    }
}

and placing the test.html file in /resources/test.html or /webapp/WEB-INF/test.html.

Every time I try to access the page at localhost:8080/some/path/test a 404 is returned.

How do I make this work?

darksmurf
  • 3,747
  • 6
  • 22
  • 38
  • try `return "test";` – Ori Marko Nov 14 '18 at 09:43
  • If I do that, the server instead throws `Circular view path [test]: would dispatch back to the current handler URL [/some/path/test] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)` – darksmurf Nov 14 '18 at 09:55
  • try return "classpath:/test.html" – Amir Aug 04 '22 at 07:32

4 Answers4

9

Okey so apparently Spring Boot supports this without any additional configuration or controllers.

All I had to do was to place the HTML file in the correct directory /resources/static/some/path/test.html and it can be reached at localhost:8080/some/path/test.html.

In my attempts to change the directory from which the file is served I was unsuccessful. It seems that providing a separate @EnableWebMvc (needed for configuring the resource handlers) breaks the Spring Boot configuration. But I can live with using the default /static directory.

darksmurf
  • 3,747
  • 6
  • 22
  • 38
1

Your html, js and css files should be under the src/main/resources/static directory. and your return statement you can try removing .html.

@RestController
public class HtmlPageController {
    @GetMapping("/some/path/test")
    public String getTestPage() {
        return "test";
    }
}
Reactive_learner
  • 417
  • 1
  • 4
  • 21
  • If I do that, the server instead throws `Circular view path [test]: would dispatch back to the current handler URL [/some/path/test] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)` – darksmurf Nov 14 '18 at 10:05
  • replace @ Controller with @ RestController and add the thymeleaf dependency. – Reactive_learner Nov 14 '18 at 12:52
  • 3
    The question specifically states that I don't want to use any template engine like Thymeleaf. – darksmurf Nov 14 '18 at 12:58
  • @RestContoller will do . No need to add any thymeleaf dependency – Reactive_learner Nov 14 '18 at 13:52
  • 1
    No it won't. `@RestController` is the same as annotating the method with `@ResponseBody`, which then will return the string "test" in the response body. – darksmurf Nov 14 '18 at 13:55
1

There is a Spring MVC mecanism that exists to provide static resources.

In the config class, overide this method :

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry
        .addResourceHandler("some/path/*.html")
        .addResourceLocations("/static/");
}

And place your html files in the src/main/webapp/static/ folder.

If you request some/path/test.html (note the .html), it will return the test.html file located in static folder.

You can obviously use a different folder or a more sofiticated directory structure.

This way you don't have to create a controller. Note that your config class should implements WebMvcConfigurer.

Romain Warnan
  • 1,022
  • 1
  • 7
  • 13
0

See tutotrial example how to define html view in Spring MVC configuration

@Bean
public InternalResourceViewResolver htmlViewResolver() {
    InternalResourceViewResolver bean = new InternalResourceViewResolver();
    bean.setPrefix("/WEB-INF/html/");
    bean.setSuffix(".html");
    bean.setOrder(2);
    return bean;
}
  • setOrder is set to 2 because it include also JSP support in example

Also you need to change to return without .html suffix

return "test.html";
Ori Marko
  • 56,308
  • 23
  • 131
  • 233