spring boot 返回字符串而不是 .html 文件

Posted

技术标签:

【中文标题】spring boot 返回字符串而不是 .html 文件【英文标题】:spring boot return string instead of .html file 【发布时间】:2019-05-10 17:14:29 【问题描述】:

我尝试运行 spring boot 应用程序,它将返回静态文件夹中的 html 静态文件, 问题是:每次我加载页面时:127.0.0.1 我得到字符串“bakara”而不是 HTML 文件 bakara.html。 当我加载 127.0.0.1/bakara.html 时,我得到了 bakara.html 文件

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>il.mda.ks</groupId>
<artifactId>mdaForm</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>mdaForm</name>
<description>Demo project for Spring Boot</description>


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.1.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>


<dependencies>
    <!-- This is a web application -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Tomcat embedded container -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- JSTL for JSP -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>

    <!-- Need this to compile JSP -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

    <!-- Need this to compile JSP, tomcat-embed-jasper version is not working, 
        no idea why -->
    <dependency>
        <groupId>org.eclipse.jdt.core.compiler</groupId>
        <artifactId>ecj</artifactId>
        <version>4.6.1</version>
        <scope>provided</scope>
    </dependency>

    <!-- Optional, test for static content, bootstrap CSS -->
    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.7</version>
    </dependency>

</dependencies>

application.properties:

#spring.mvc.view.prefix=/static/
#spring.mvc.view.suffix=.html

spring.mvc.view.prefix=/static
spring.mvc.view.suffix=.html

HomeController.java:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class HomeController 

    @RequestMapping("/")
    @ResponseBody
    public String welcome() 
        return "bakara";
    

项目结构:

|── src
│   ├── main
│   │   ├── java
│   │   │   └── il
│   │   │       └── mda
│   │   │           └── ks
│   │   │               └── mdaForm
│   │   │                   ├── BakaraController.java
│   │   │                   ├── HomeController.java
│   │   │                   └── MdaFormApplication.java
│   │   └── resources
│   │       ├── application.properties
│   │       ├── static
│   │       │   ├── assets
│   │       │   ├── bakara.html
│   │       │   ├── succeed.html
│   │       │   └── TokenDenied.html
│   │       └── templates

【问题讨论】:

【参考方案1】:

@Controller VS @RestController

@Controller 用于将类标记为 Spring MVC Controller。 @RestController 是一个方便的注解,它只是添加了@Controller@ResponseBody 注解。

因此,在您的情况下,只需从 HomeController.java 中的 welcome() 方法中删除 @ResponseBody 注释,就足以获得所需的输出。

还可以查看此 Spring 指南,了解如何使用 Serve Web Content with Spring MVC

【讨论】:

【参考方案2】:

默认情况下,Spring Boot 在 templates 文件夹中查找您的 html 模板 static 文件夹是您的其他文件,例如 css and js 。尝试将您的 html 文件移动到 src/main/resources/templates 文件夹中并从控制器方法中删除 @ResponseBody并将其从您的应用程序属性spring.mvc.view.prefix=/static 中删除。我希望它会起作用。

【讨论】:

【参考方案3】:

当你使用注解@ResponseBody 时,你实际上是在告诉spring 不要试图用返回的名字去寻找一个视图。如果您想要 html,只需从控制器方法中删除注释即可。

【讨论】:

我认为您使用 spring.mvc.view.prefix=/static 配置了 application.properties。您是否尝试通过127.0.0.1/static/bakara.html 访问该页面?【参考方案4】:

我遇到了同样的问题。仅通过返回带有 html 文件名称的 String 是无法返回 html 的。对我来说,它只能使用 ModelAndView,在你的情况下它看起来像这样:

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

有人说出现这样的问题是因为jdk版本。我用jdk14。

【讨论】:

使用@Controller而不是@RestController

以上是关于spring boot 返回字符串而不是 .html 文件的主要内容,如果未能解决你的问题,请参考以下文章