Java 尝试浏览到rest控制器时调用Spring Boot 404

Java 尝试浏览到rest控制器时调用Spring Boot 404,java,spring-boot,spring-restcontroller,spring-rest,Java,Spring Boot,Spring Restcontroller,Spring Rest,我在这里看到了许多类似的问题,但似乎没有一个是我的解决方案。我的包结构似乎是正确的,即使不正确,在应用程序类中显式声明包或@RestController类仍然会导致404错误。我注意到我的问题与我见过的大多数其他问题不同的一点是,我得到的是HTML404响应,而我见过的大多数其他问题得到的是JSON404响应。我没有部署这个应用程序,只是使用mvn-spring-boot:run运行 这是我的密码: package com.billbuddy; import org.springframewo

我在这里看到了许多类似的问题,但似乎没有一个是我的解决方案。我的包结构似乎是正确的,即使不正确,在应用程序类中显式声明包或
@RestController
类仍然会导致404错误。我注意到我的问题与我见过的大多数其他问题不同的一点是,我得到的是HTML404响应,而我见过的大多数其他问题得到的是JSON404响应。我没有部署这个应用程序,只是使用
mvn-spring-boot:run
运行

这是我的密码:

package com.billbuddy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BillbuddyApplication {

    public static void main(String[] args) {
        SpringApplication.run(BillbuddyApplication.class, args);
    }
}
添加注释
@ComponentScan(@ComponentScan(basePackageClasses=UserController.class)
仍然会导致404

这是我的rest控制器类

package com.billbuddy.controllers;

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

@RestController
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login")
    public String login() {
        return "Hello, World";
    }
}
正如您所看到的,我的Rest控制器位于Spring应用程序类的嵌套类中

- src/main/java
  - com/billbuddy
    - BillbuddyApplication.java
    - controllers
        - UserController.java
这是我的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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.testprojects</groupId>
    <artifactId>billbuddy</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>billbuddy</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-data-couchbase</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </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>
我正在测试
@RestController
,方法是在chrome中浏览到,并查看以下屏幕:

运行

curl http://localhost:8080/user/login 
返回

<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1></body></html>
HTTP状态404–未找到h1{字体系列:Tahoma,Arial,无衬线;颜色:白色;背景色:525D76;字体大小:22px;}h2{字体系列:Tahoma,Arial,无衬线;颜色:白色;背景色:525D76;字体大小:16px;}h3{字体系列:Tahoma,Arial,无衬线;颜色:白色;背景色:525D76;字体大小:14px;}正文{字体系列:Tahoma,Arial,无衬线;颜色:黑色;背景色:白色;}b{字体系列:Tahoma,Arial,无衬线;颜色:白色;背景色:}p{字体系列:Tahoma,Arial,无衬线;背景色:白色;颜色:黑色;字体大小:12px;}a{颜色:黑色;}a.name{颜色:黑色;}线条{高度:1px;背景色:#d76;边框:无;}HTTP状态404–未找到

我肯定这会是我在做的蠢事,但我已经尝试了几十个在stack overflow和其他网站上看到的其他答案,但都无济于事。请帮助我。

在我的电脑上对一个项目进行了快速测试…你的pom中应该有以下依赖项:

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

org.springframework.boot
SpringBootStarterWeb

让我知道它是否有效。

在我的电脑上对一个项目进行了快速测试……您的pom中应该有以下依赖项:

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

org.springframework.boot
SpringBootStarterWeb

让我知道它是有效的。< / P>是的,非常感谢!这是一个两小时的斗争,我欢迎你。下次考虑使用一个完整的POM.XML来为你的项目做准备,它应该能帮助你避免这种恼人的问题。是的,非常完美。非常感谢,这是一个两小时的斗争,我欢迎你。下次考虑使用GE。如果您的项目没有完整的pom.xml,它应该可以帮助您避免此类恼人的问题。