@RequestMapping spring boot 无法按预期运行 [英] @RequestMapping spring boot doesn't function as expected

查看:71
本文介绍了@RequestMapping spring boot 无法按预期运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个打印Hello World"的简单应用程序.到端口 8080

I am trying to create a simple application that prints "Hello World" to port 8080

这是我的主要 Java 类:

Here is my main java class:

package com.iz.backend;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;

@SpringBootApplication

public class BackendApplication {

@RequestMapping("/")
public String helloWorld() {
    return "Hello World";
}

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

代码编译成功,应用程序构建成功,但是当我访问 localhost:8080 时

The code compiles and the app builds successfully but when I go to localhost:8080

我收到 404 错误:

I get 404 error:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon Jan 11 21:18:51
There was an unexpected error (type=Not Found, status=404).

我的 pom.xml 有什么问题吗?这是 pom 文件:

Is there anything wrong with my pom.xml? here is the pom file:

<?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.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.iz</groupId>
    <artifactId>backend</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>backend</name>
    <description>Demo project for Spring Boot</description>

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

    <dependencies>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>2.4</version>
        </dependency>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>compile</scope>
        </dependency>

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

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

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

</project>

推荐答案

添加到@Reimeus 答案,

Adding to @Reimeus answer,

确保将 MyController 类和 BackendApplication 类保留在主包中,即 com.iz.backend.

Make sure to keep MyController class and BackendApplication class within main package i.e, com.iz.backend.

 |-src/main/java
    |--com.iz.backend 
       |--controllers
          |--MyController
       |--BackendApplication

或者,使用 basePackages:

Or, use basePackages:

@SpringBootApplication(scanBasePackages = {"com.iz.backend"}) 

这篇关于@RequestMapping spring boot 无法按预期运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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