如何在 Spring Boot 中从一个安静的控制器返回一个 html 页面? [英] How to return a html page from a restful controller in spring boot?

查看:25
本文介绍了如何在 Spring Boot 中从一个安静的控制器返回一个 html 页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to return a simple html page from controller, but I get only the name of the file not its content. Why?

这是我的控制器代码:

@RestController
public class HomeController {

    @RequestMapping("/")
    public String welcome() {
        return "login";
    }
}

这是我的项目结构:

[

推荐答案

当像这样使用 @RestController 时:

@RestController
public class HomeController {

    @RequestMapping("/")
    public String welcome() {
        return "login";
    }
}

这与您在普通控制器中所做的相同:

This is the same as you do like this in a normal controller:

@Controller
public class HomeController {

    @RequestMapping("/")
    @ResponseBody
    public String welcome() {
        return "login";
    }
}

使用 @ResponseBody 返回 return "login"; 作为 String 对象.您返回的任何对象都将作为 payload 作为 JSON 附加到 HTTP 正文中.

Using @ResponseBody returns return "login"; as a String object. Any object you return will be attached as payload in the HTTP body as JSON.

这就是为什么您在响应中只得到 login.

This is why you are getting just login in the response.

这篇关于如何在 Spring Boot 中从一个安静的控制器返回一个 html 页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆