Return HTML page from Spring controller










-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?










share|improve this question






















  • try return "test";

    – user7294900
    Nov 14 '18 at 9: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 9:55















-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?










share|improve this question






















  • try return "test";

    – user7294900
    Nov 14 '18 at 9: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 9:55













-1












-1








-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?










share|improve this question














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?







java spring spring-mvc spring-boot






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 9:35









darksmurfdarksmurf

81611118




81611118












  • try return "test";

    – user7294900
    Nov 14 '18 at 9: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 9:55

















  • try return "test";

    – user7294900
    Nov 14 '18 at 9: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 9:55
















try return "test";

– user7294900
Nov 14 '18 at 9:43





try return "test";

– user7294900
Nov 14 '18 at 9: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 9:55





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 9:55












4 Answers
4






active

oldest

votes


















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.






share|improve this answer






























    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";







    share|improve this answer

























    • 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






    • 2





      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











    • 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


















    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";





    share|improve this answer






























      0














      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.






      share|improve this answer






















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296960%2freturn-html-page-from-spring-controller%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        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.






        share|improve this answer



























          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.






          share|improve this answer

























            1












            1








            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 14 '18 at 10:15









            Romain WarnanRomain Warnan

            57337




            57337























                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";







                share|improve this answer

























                • 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






                • 2





                  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











                • 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














                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";







                share|improve this answer

























                • 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






                • 2





                  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











                • 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












                1








                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";







                share|improve this answer















                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";








                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 14 '18 at 12:51

























                answered Nov 14 '18 at 9:43









                Reactive_learnerReactive_learner

                717




                717












                • 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






                • 2





                  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











                • 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

















                • 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






                • 2





                  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











                • 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
















                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





                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





                replace @ Controller with @ RestController and add the thymeleaf dependency.

                – Reactive_learner
                Nov 14 '18 at 12:52




                2




                2





                The question specifically states that I don't want to use any template engine like Thymeleaf.

                – darksmurf
                Nov 14 '18 at 12:58





                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





                @RestContoller will do . No need to add any thymeleaf dependency

                – Reactive_learner
                Nov 14 '18 at 13:52













                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





                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











                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";





                share|improve this answer



























                  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";





                  share|improve this answer

























                    0












                    0








                    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";





                    share|improve this answer













                    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";






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 '18 at 10:10









                    user7294900user7294900

                    22k113258




                    22k113258





















                        0














                        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.






                        share|improve this answer



























                          0














                          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.






                          share|improve this answer

























                            0












                            0








                            0







                            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.






                            share|improve this answer













                            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.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 14 '18 at 12:07









                            darksmurfdarksmurf

                            81611118




                            81611118



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296960%2freturn-html-page-from-spring-controller%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Top Tejano songwriter Luis Silva dead of heart attack at 64

                                ReactJS Fetched API data displays live - need Data displayed static

                                Evgeni Malkin