spring boot return string instead of .html file

后端 未结 4 2035
无人共我
无人共我 2021-02-15 10:59

I tried to run spring boot application, that will return me the HTML static file on the static folder, the problem was: every time I load the page: 127.0.0.1 I get the String \"

相关标签:
4条回答
  • 2021-02-15 11:24

    @Controller VS @RestController

    • @Controller is used to mark classes as Spring MVC Controller.
    • @RestController is a convenience annotation that does nothing more than adding the @Controller and @ResponseBody annotations.

    So in your case just removing the @ResponseBody annotation from the welcome() method in HomeController.java, should be enough to get the desired output.

    Also have a look at this Spring Guide displaying how to Serve Web Content with Spring MVC

    0 讨论(0)
  • 2021-02-15 11:27

    I had this same problem. There was no way to return an html just by returning a String with the name of the html file. For me, it only worked using ModelAndView, in your case it would look like this:

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

    Some people say that problems like this occurs because the jdk version. I use jdk14.

    0 讨论(0)
  • 2021-02-15 11:30

    By Default Spring Boot Looks For your html templates in templates folder static folder is for your other files like css and js .Try moving your html files in src/main/resources/templates folder and remove @ResponseBody from your controller method and remove this from your application properties spring.mvc.view.prefix=/static. I hope it will work.

    0 讨论(0)
  • 2021-02-15 11:38

    When you use annotation @ResponseBody, you actually tell spring to not try to find a view with the returned name. If you want the html, just remove the annotation from the controller method.

    0 讨论(0)
提交回复
热议问题