【问题标题】:I cant start my spring boot project, error 404 when i run我无法启动我的 Spring Boot 项目,运行时出现错误 404
【发布时间】:2021-01-13 03:26:13
【问题描述】:

这是我的控制器,应该打开索引页

@Controller
public class index {
    
    @RequestMapping("/index")
    public String index() {
        return "index";
    }
}

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 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.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zetcode</groupId>
    <artifactId>cinemaweb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cinemaweb</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</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</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.1.2.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

这些是项目目录

当我运行我的项目浏览器时,显示“HTTP 状态 404 – 未找到”,我阅读了其他帖子,但我没有找到解决方法,请帮帮我

我尝试使用这 2 个链接 http://localhost:8080/cinemaweb 或 http://localhost:8080/index

【问题讨论】:

  • 这个post 对你有帮助。请阅读编辑部分。
  • 能否分享 application.properties 文件,或者是否设置了 server.servlet.context-path 的值
  • 我的 application.properties 文件为空

标签: java spring-boot pom.xml


【解决方案1】:

使用@RestController 而不是@Controller

@RestController
public class index
{
    @GetMapping("/index")
    public String index()
    {
        return "index";
    }
}

【讨论】:

    【解决方案2】:
    1. 最好使用@RestController 而不是@Controller
    2. 然后在方法上使用@GetMapping(value = "/index") - 而不是@RequestMapping 那么网址是:http://localhost:8080/index

    【讨论】:

      【解决方案3】:

      如果你想返回一个String作为一个Object,那么使用@RestController或者@ResonseBody注解,像这样

      @Controller
      public class MainController {
      
      @RequestMapping(value = "/", method = RequestMethod.GET)
      public @ResponseBody String get() {
          return "index";
      }
      

      }

      另外,我强烈建议您将 spring-web 依赖替换为 spring-boot-starter-web,我 意思是这个

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

      Spring boot 会处理所有必需的 bean,例如调度程序 servlet 等。

      【讨论】:

        猜你喜欢
        • 2017-08-26
        • 1970-01-01
        • 2019-04-26
        • 2020-10-13
        • 1970-01-01
        • 2019-03-25
        • 1970-01-01
        • 2019-08-25
        • 1970-01-01
        相关资源
        最近更新 更多