春季启动 REST API 404 错误

Posted

技术标签:

【中文标题】春季启动 REST API 404 错误【英文标题】:Spring boot REST API 404 error 【发布时间】:2018-10-05 00:11:48 【问题描述】:

我正在尝试使用 REST API 创建一个基本的 Spring Boot 应用程序 (JDK 1.8)。 以下是我的应用代码

package org.demo;

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

@SpringBootApplication
@ComponentScan("org.demo")
@EnableAutoConfiguration
public class DemoApplication 

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

我添加了一个简单的控制器如下

    package org.demo.controllers;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController(value = "/hello")
public class HelloController 

    @GetMapping
    @ResponseBody
    public String hello() 
        return "Hello";
    

我的项目的POM文件如下

    <?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>

    <groupId>org.demo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <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>
        </dependency>

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

    </dependencies>

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


</project>

当我运行这个应用程序时,它会成功启动。但是,当我尝试访问 URL localhost:8080 或 loclahost:8080/hello 时,我收到 404 错误消息

以下是启动日志 :: Spring Boot :: (v2.0.1.RELEASE)

2018-04-24 21:51:38.888  INFO 12068 --- [           main] org.demo.DemoApplication                 : Starting DemoApplication on DESKTOP-G2QR23G with PID 12068 (I:\demo\spring-boot\demo\target\classes started by chirayu in I:\demo\spring-boot\demo)
2018-04-24 21:51:38.892  INFO 12068 --- [           main] org.demo.DemoApplication                 : No active profile set, falling back to default profiles: default
2018-04-24 21:51:38.970  INFO 12068 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5745ca0e: startup date [Tue Apr 24 21:51:38 IST 2018]; root of context hierarchy
2018-04-24 21:51:39.617  INFO 12068 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-04-24 21:51:39.999  INFO 12068 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-04-24 21:51:40.030  INFO 12068 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-04-24 21:51:40.030  INFO 12068 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.29
2018-04-24 21:51:40.043  INFO 12068 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [I:\Program Files\Java\jdk1.8.0_66\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;I:\Program Files\Java\jdk1.8.0_66\bin;I:\Program Files\nodejs\;C:\Program Files (x86)\GtkSharp\2.12\bin;C:\Program Files (x86)\Skype\Phone\;I:\MinGW\bin;C:\Program Files (x86)\mysql\MySQL Fabric 1.5 ^& MySQL Utilities 1.5\;C:\Program Files (x86)\MySQL\MySQL Fabric 1.5 ^& MySQL Utilities 1.5\Doctrine extensions for php\;I:\Program Files (x86)\maven\apache-maven-3.3.9\bin\;I:\Program Files (x86)\Heroku\bin;C:\Program Files (x86)\git\cmd;I:\Program Files\Git\cmd;I:\Program Files\PostgreSQL\9.6\bin;I:\RailsInstaller\Git\cmd;I:\RailsInstaller\Ruby2.2.0\bin;I:\Program Files (x86)\Python\Scripts\;I:\Program Files (x86)\Python\;C:\Users\chirayu\AppData\Roaming\npm;C:\Users\chirayu\AppData\Local\Microsoft\WindowsApps;I:\Program Files (x86)\Microsoft VS Code\bin;;.]
2018-04-24 21:51:40.164  INFO 12068 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-04-24 21:51:40.164  INFO 12068 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1198 ms
2018-04-24 21:51:40.215  INFO 12068 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-04-24 21:51:40.598  INFO 12068 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-04-24 21:51:40.633  INFO 12068 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2018-04-24 21:51:40.638  INFO 12068 --- [           main] org.demo.DemoApplication                 : Started DemoApplication in 2.2 seconds (JVM running for 2.797)

我不确定我可能会错过什么。请帮忙。

【问题讨论】:

尝试将@RestController(value = "/hello")更改为@RestController @RequestMapping("/hello") 去掉@ComponentScan("org.demo")@EnableAutoConfiguration注解,因为@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan 如果你想扫描一些特定的包你可以使用@EntityScan(basePackages = "org.demo")而不是@ComponentScan("org.demo") 我不确定问题出在哪里,但是当我将我的应用程序基础包 (org.demo) 重构为其他名称 (org.demo.app) 时,同一个控制器开始工作。现在,当我将基本包重构为原始(org.demo)名称时,它仍然可以工作 【参考方案1】:

我得到了解决方案。这是由于包裹的可见性。主类无法找到控制器类所在的包。所以在同一个包下添加了所有类。您还可以将应用程序类提高一级。从以下链接获得帮助:

Spring Boot: Cannot access REST Controller on localhost (404)

【讨论】:

我的主类和控制器包已经在同一级别了。至于将所有类放在同一个包下,这可能有效,但我更感兴趣的是为什么我的配置不起作用。【参考方案2】:

测试如下...对我来说,从@RestController 符号中删除路径然后添加路径@GetMapping 符号如下@GetMapping("/hello") test http://localhost:8080/hello you应该得到正确的响应才能得到这个请求的响应 http://localhost:8080/hello/hello 需要添加以下内容:

@RestController
@RequestMapping(value = "/hello")

【讨论】:

【参考方案3】:

改为:

@RestController
@RequestMapping(value = "hello-controller")
public class HelloController 

@GetMapping(value="hello")
@ResponseBody
public String hello() 
    return "Hello";


所以你可以使用你的 api:localhost:8080/hello-controller/hello

    @RequestMapping 下面@RestController 中的值是定义你的班级地址。 @GetMapping 中的值用于您的 API 地址。

希望这个解释对你有所帮助。

【讨论】:

以上是关于春季启动 REST API 404 错误的主要内容,如果未能解决你的问题,请参考以下文章

Wordpress REST API (wp-api) 404 错误:无法访问 WordPress REST API

spring boot rest api测试得到404错误

REST API 404:错误的 URI 或缺少资源?

运行 REST API 时找不到 Tomcat 错误 404

Spring Boot REST API war 文件生成 404 错误

Paypal Refund Sale REST API 返回:远程服务器返回错误:(404)未找到