简体   繁体   English

简单的 Spring 引导项目给出 404 错误

[英]Simple Spring Boot Project is giving 404 Error

I am learning Spring Boot but I am not able to execute the basic "Hello World" program.我正在学习 Spring 引导,但我无法执行基本的“Hello World”程序。 I have built the basic project using https://start.spring.io/ and updated the code as per below code snippets:我已经使用https://start.spring.io/构建了基本项目,并根据以下代码片段更新了代码:

Spring Boot Main Class: Spring 引导主 Class:

package com.rohit.springboot.practice.selfspringboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication
@ComponentScan(basePackages = "com.rohit.springboot.practice.selfspringboot")
public class SelfSpringBootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SelfSpringBootApplication.class, args);
    }

}

Controller Class: Controller Class:

package com.rohit.springboot.practice.selfspringboot;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ThisWorldSpringContainer {
    
    
    @RequestMapping(method = RequestMethod.GET, value = "/hello-world")
    public String getWelcome() {
        
        return "Hello World";
    }

}

POM.xml file: 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.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.rohit.springboot.practice</groupId>
    <artifactId>self-spring-boot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>self-spring-boot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

Error:错误:

Message: The requested resource [/hello-world] is not available

I know this question has been asked by multiple people but i have gone through those answers and tried those given answers but didn't work.我知道这个问题已经被多人问过,但我已经阅读了这些答案并尝试了那些给出的答案,但没有奏效。

Technologies used are Java 8, Tomcat 9, Spring Boot 2.6.6 etc.使用的技术有 Java 8、Tomcat 9、Spring Boot 2.6.6 等。

Kindly help.请帮忙。

Since you don't have the Tomcat dependency, I'm implying that you want your application to run in a standalone Tomcat instance.由于您没有 Tomcat 依赖项,因此我暗示您希望您的应用程序在独立的 Tomcat 实例中运行。 Also, given your comment, I believe you're working on either Eclipse or Spring Tools Suite (Which is the same, really).另外,鉴于您的评论,我相信您正在研究 Eclipse 或 Spring 工具套件(实际上是相同的)。

With that, it seems your problem is the lack of a packaging method.这样一来,您的问题似乎是缺少打包方法。 Tomcat needs the code its going to run in a package. Tomcat 需要在 package 中运行的代码。 This package is a zip-like file with .war extension.这个 package 是一个类似 zip 的文件,扩展名为.war Maven, the program that handles the dependencies of your project according to the pom.xml file, can package it in either war of jar format. Maven, the program that handles the dependencies of your project according to the pom.xml file, can package it in either war of jar format. Since you are running it in an external Tomcat, you want the war extension.由于您在外部 Tomcat 中运行它,因此您需要war扩展。 So please, add the following line to your pom.xml file.因此,请将以下行添加到您的pom.xml文件中。 It has to be in the same level as <name> :它必须与<name>处于同一级别:

<packaging>war</packaging>

Then, update your project (Alt + F5, check your project and hit OK) then try running your project again.然后,更新您的项目(Alt + F5,检查您的项目并点击确定),然后再次尝试运行您的项目。 You should see an stylish "SPRING" output in your console.您应该在控制台中看到一个时尚的“SPRING”output。

Now, since you are running this externally, you need to specify the name of the project, so you should call your endpoint with现在,由于您在外部运行它,您需要指定项目的名称,因此您应该使用

http://localhost:8080/<your_project_name_here>/hello-world

I advise you to check this new change in Spring Boot 2.6:我建议您检查 Spring Boot 2.6 中的这一新变化:

PathPattern Based Path Matching Strategy for Spring MVC 基于 PathPattern 的 Spring MVC 路径匹配策略

In this release, the default strategy for matching request paths against registered Spring MVC handler mappings has changed from AntPathMatcher to PathPatternParser .在此版本中,将请求路径与已注册的 Spring MVC 处理程序映射匹配的默认策略已从AntPathMatcher更改为PathPatternParser

If you need to switch the default strategy back to AntPathMatcher , you can set the property spring.mvc.pathmatch.matching-strategy to ant-path-matcher as in the line below to add in the file application.properties :如果您需要将默认策略切换回AntPathMatcher ,您可以将属性spring.mvc.pathmatch.matching-strategy设置为ant-path-matcher ,如下行所示添加到文件application.properties中:

spring.mvc.pathmatch.matching-strategy=ant-path-matcher

I encountered the same issue of HTTP 404 error when upgrading to Spring Boot 2.6 and adding this property solved my problem and should solve yours.升级到 Spring Boot 2.6 并添加此属性时,我遇到了 HTTP 404 错误的相同问题并添加此属性解决了我的问题,应该也解决了你的问题。

I have modified your code as below:我已将您的代码修改如下:

@RequestMapping
@RestController
public class ThisWorldSpringContainer {
    
    @GetMapping(value = "/hello-world")
    public String getWelcome() {
        
        return "Hello World";
    }

}

I thinking you are missing我想你不见了

@Requestmapping

in classlevel在班级水平

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM