Java 如何在spring boot中从restful控制器返回html页面?

Java 如何在spring boot中从restful控制器返回html页面?,java,html,spring,spring-boot,controller,Java,Html,Spring,Spring Boot,Controller,我想从控制器返回一个简单的html页面,但我只得到文件名,而不是它的内容。为什么? 这是我的控制器代码: @RestController public class HomeController { @RequestMapping("/") public String welcome() { return "login"; } } 这是我的项目结构: [您在此处返回的字符串: return "login"; 实际上是您发送回浏览器的全部内容 如果要将

我想从控制器返回一个简单的html页面,但我只得到文件名,而不是它的内容。为什么?

这是我的控制器代码:

@RestController
public class HomeController {

    @RequestMapping("/")
    public String welcome() {
        return "login";
    }
}
这是我的项目结构:


[

您在此处返回的
字符串:

 return "login";
实际上是您发送回浏览器的全部内容

如果要将文件内容发回,一种方法是:

  • 打开文件
  • 将内容读入字符串
  • 返回值

  • 您可以使用此

    只获取名称,因为您只返回名称
    返回“login”
    。它是
    @RestController
    ,此控制器返回数据而不是视图;因此,您只获取从方法返回的内容


    如果要使用此名称显示视图,则需要使用Spring MVC,.

    当使用
    @RestController
    时,如下所示:

    @RestController
    public class HomeController {
    
        @RequestMapping("/")
        public String welcome() {
            return "login";
        }
    }
    
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
      <version>2.4.4</version>
    </dependency>
    
    这与在普通控制器中执行的操作相同:

    @Controller
    public class HomeController {
    
        @RequestMapping("/")
        @ResponseBody
        public String welcome() {
            return "login";
        }
    }
    
    @RestController
    public class MyRestController {
        @RequestMapping("/")
        public ModelAndView welcome() {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("login.html");
            return modelAndView;
        }  
    }
    
    @Controller
    public class MyController {
    
      //gets html from a default 'resources/public' or 'resources/static' folder
      @RequestMapping(path="/welcome")
      public String getWelcomePage(){
          return "login.html";
      }
    
      //gets html from a default 'resources/public' or 'resources/static' folder
      @RequestMapping("/welcome1")
      public ModelAndView getWelcomePageAsModel() {
          ModelAndView modelAndView = new ModelAndView();
          modelAndView.setViewName("login.html");
          return modelAndView;
      }
    
      //  fails with 404 resource not found by default
      //  NO fail, if spring mvc view suffix is set in properties e.g.: spring.mvc.view.suffix=.html
      //  NO fail, if thymeleaf is added, and there is a file login.html in a resources/templates folder
      @RequestMapping(path="/welcome2")
      public String thisFails(){
          return "login";
      }
    }
    
    使用
    @ResponseBody
    返回
    返回“登录”
    作为字符串对象。返回的任何对象都将作为
    有效负载作为JSON附加到HTTP正文中


    这就是为什么您在响应中只得到
    登录名

    您可以尝试使用
    模型和视图

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

    @RestController
    替换为
    @Controller

    @RestController
    替换为
    @Controller
    @RestController
    仅返回内容,不返回html和jsp页面。

    您应该使用
    login.html
    ,并将
    RestController
    替换为
    Controller

    Kukkuz的er不适用于我,直到我在pom文件中添加了此依赖项:

    <!-- Spring boot Thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
    
    org.springframework.boot
    

    以及更新所概述的注册表资源


    然后它就开始正常工作了。如果将来有人遇到同样的问题,而遵循第一个答案并不能解决您的问题,那么在使用@Kukkuz答案中的代码后,遵循以下两个其他步骤,看看这是否会产生影响。

    最正确和现代的形式是使用
    IoC
    将依赖项放入端点方法,如thymeleaf
    模型
    实例

     @Controller 
     public class GreetingController {
            @GetMapping("/greeting") 
            public String greeting(
                @RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
                 model.addAttribute("name", name); 
                 return "greeting"; 
                 // returns the already proccessed model from src/main/resources/templates/greeting.html 
          }
     }
    

    请参阅完整示例:

    我没有使用Thymeleaf,它对我有效的唯一方式是使用
    @Controller
    在课堂上

    @RequestMapping(method = RequestMethod.GET, value = "/")
    public String index() {
        return "index.html";
    }
    
    请遵循以下步骤:

  • 必须将html文件放入资源/模板中/

  • @RestController
    替换为
    @Controller

  • 如果正在使用任何视图解析程序,请删除

  • 控制器方法应该返回视图的文件名,不带扩展名,如
    返回“index”

  • 包括以下依赖项:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>`
    
    
    org.springframework.boot
    SpringBootStarterWeb
    org.springframework.boot
    弹簧启动装置
    org.springframework.boot
    弹簧靴开发工具
    `
    

  • 您可以通过两种方式解决此问题:

    第一种方法:如果您希望继续使用REST,您必须使用
    model和view
    对象来呈现HTML页面。Happy Nguyen发布了一个示例,我在这里再次发布:

    @RequestMapping("/")
    public ModelAndView index () {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index");
        return modelAndView;
    }
    
    第二种方法:如果您是否继续使用REST并不重要,那么您可以将
    @RestController
    更改为
    @Controller
    ,并确保您已经添加了Thymeleaf模板引擎

    @Controller
    public class WebController {
    @GetMapping("/")
    
    public String homePage() {
        return "index";
      }
    }
    
    这将返回Login.html文件。Login.html应位于静态文件夹中

    注意:未添加thymeleaf依赖项

    @RestController
    public class HomeController {
    
        @RequestMapping(method = RequestMethod.GET, value = "/")
        public String welcome() {
            return "login";
        }
    }
    

    这将返回登录字符串

    我知道这很旧,但可能对某人有所帮助。要在RestController中执行此操作,请返回一个实际HTML代码字符串,然后浏览器将知道显示什么。

    我做了三件事:

    • 将HTML页面放入{project.basedir}/resources/static/myPage.HTML
    • 将@RestController切换到@Controller
    • 这是我的控制器:

    不需要特别的依赖关系。

    @kukkuz几乎回答了“为什么?”这个问题。对于那些仍在研究“如何”的人,积累其他人的答案

    使用RestController:

    @Controller
    public class HomeController {
    
        @RequestMapping("/")
        @ResponseBody
        public String welcome() {
            return "login";
        }
    }
    
    @RestController
    public class MyRestController {
        @RequestMapping("/")
        public ModelAndView welcome() {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("login.html");
            return modelAndView;
        }  
    }
    
    @Controller
    public class MyController {
    
      //gets html from a default 'resources/public' or 'resources/static' folder
      @RequestMapping(path="/welcome")
      public String getWelcomePage(){
          return "login.html";
      }
    
      //gets html from a default 'resources/public' or 'resources/static' folder
      @RequestMapping("/welcome1")
      public ModelAndView getWelcomePageAsModel() {
          ModelAndView modelAndView = new ModelAndView();
          modelAndView.setViewName("login.html");
          return modelAndView;
      }
    
      //  fails with 404 resource not found by default
      //  NO fail, if spring mvc view suffix is set in properties e.g.: spring.mvc.view.suffix=.html
      //  NO fail, if thymeleaf is added, and there is a file login.html in a resources/templates folder
      @RequestMapping(path="/welcome2")
      public String thisFails(){
          return "login";
      }
    }
    
    • 请注意,视图名称为:“login.html”(完整文件名)
    • 此外,文件的位置也很重要,默认情况下login.html必须位于resources/static或resources/public中
    您可以为默认后缀设置应用程序参数,如:

     spring.mvc.view.suffix=.html
    
    在这种情况下,视图名称必须没有像“login”这样的扩展名

    可以使用一些建议的thymeleaf。 意味着您在pom.xml依赖项中有如下内容:

    @RestController
    public class HomeController {
    
        @RequestMapping("/")
        public String welcome() {
            return "login";
        }
    }
    
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
      <version>2.4.4</version>
    </dependency>
    
    使用控制器:

    @Controller
    public class HomeController {
    
        @RequestMapping("/")
        @ResponseBody
        public String welcome() {
            return "login";
        }
    }
    
    @RestController
    public class MyRestController {
        @RequestMapping("/")
        public ModelAndView welcome() {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("login.html");
            return modelAndView;
        }  
    }
    
    @Controller
    public class MyController {
    
      //gets html from a default 'resources/public' or 'resources/static' folder
      @RequestMapping(path="/welcome")
      public String getWelcomePage(){
          return "login.html";
      }
    
      //gets html from a default 'resources/public' or 'resources/static' folder
      @RequestMapping("/welcome1")
      public ModelAndView getWelcomePageAsModel() {
          ModelAndView modelAndView = new ModelAndView();
          modelAndView.setViewName("login.html");
          return modelAndView;
      }
    
      //  fails with 404 resource not found by default
      //  NO fail, if spring mvc view suffix is set in properties e.g.: spring.mvc.view.suffix=.html
      //  NO fail, if thymeleaf is added, and there is a file login.html in a resources/templates folder
      @RequestMapping(path="/welcome2")
      public String thisFails(){
          return "login";
      }
    }
    

    在用@RestController注释的类中,用@GetMapping注释的方法可以通过两种方式返回视图(JSP、HTML等)

    @GetMapping("/dummy")
    public String renderDummyWebPage() {
        return "dummy.jsp";
    }
    
    但是,此方法有两个限制

  • 该JSP页面必须存在于src/main/webapp目录中,否则,如果视图存在于子目录中,则必须相应地修改返回类型。例如,如果JSP页面存在于src/main/webapp/views目录中,则应将视图/dummy.JSP作为字符串返回。

  • 如果您已经配置了返回简单字符串的应用程序的ViewResolver,则该字符串将不起作用,并且该字符串将被解释为普通字符串而不是JSP视图名称


  • 为了克服这两个限制,您可以返回ModelAndView

    @GetMapping("/dummy")
    public ModelAndView renderDummyWebPage() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("dummy");
        return modelAndView;
    }
    


    我希望这个答案会有帮助,谢谢。

    有任何描述吗?这也适用于spring.mvc.view.prefix:/WEB-INF/jsp/spring.mvc.view.suffix:.jsp在添加
    spring boot starter thymeleaf
    依赖项后,视图被删除