【问题标题】:Spring boot controller issue, getting whitelabel page error ,what might be wrong here?Spring Boot 控制器问题,出现白标签页面错误,这里可能有什么问题?
【发布时间】:2020-06-27 07:25:45
【问题描述】:

我之前在春季做过项目,之前从未遇到过这样的问题,我似乎无法弄清楚这里出了什么问题,所有依赖项都正确导入。

这是我的 vaultengine.java 的样子:

VaultEngine.java:

package com.example.VaultEngine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@Controller
public class VaultEngineApplication {

public static void main(String[] args) {
    SpringApplication.run(VaultEngineApplication.class, args);
}
@GetMapping("/h")
public String greetingForm() {
    return "index.html";
}
}

我正在使用自定义端口,因为我已经在某处使用了 8080。 application.properties:

server.port=8989

所有依赖项都已正确加载。 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 https://maven.apache.org/xsd/maven- 
   4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
   <groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <version>2.2.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.19</version>
    </dependency>


    </dependencies>
    <build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>com.google.cloud.tools</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>1.3.2</version>
            <configuration>
                <version>1</version>
            </configuration>
        </plugin>

    </plugins>
  </build>

 </project>

【问题讨论】:

  • 分享您的 index.html 文件位置及其代码。我认为您的 index.html 可能放错了位置。

标签: spring spring-boot spring-mvc


【解决方案1】:

你使用的是spring-boot-starter-thymeleaf,所以

    @GetMapping("/h")
    public String greetingForm() {
        return "index";
    }

并将index.html 放入src/main/resources/templates/

参考:

【讨论】:

    【解决方案2】:

    使用@RestController 而不是@Controller

    @SpringBootApplication
    @RestController
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
        @GetMapping("/h")
        public String greetingForm() {
            return "index.html";
        }
    }
    

    【讨论】:

    • 为什么?看起来这里不需要其他细节。
    猜你喜欢
    • 2021-07-31
    • 2016-08-04
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多