You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot REST接口出现HTTP 404错误,请求资源不可用求助

Spring Boot REST接口404错误排查与解决

问题描述

创建了一个Spring Boot REST应用,通过「Run on server」运行时,浏览器会重定向到包含context-path的URL,但调用自定义接口时抛出HTTP 404 - 请求资源不可用错误,尝试多种方法未解决。

控制器代码

package in.ineuron.controller;

import java.time.LocalDateTime;

import org.springframework.http.HttpStatus;    
import org.springframework.http.HttpStatusCode;    
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import in.ineuron.response.CurrencyResponse;

@RestController
@RequestMapping("/api/currency")
public class CurrencyController {

@GetMapping("/getCurrencyValue/from/{from}/to/{to}")
public ResponseEntity<CurrencyResponse> getCurrencyValue(@PathVariable String from,
        @PathVariable String to){
    CurrencyResponse response=new CurrencyResponse();
    response.setCurrencyId(1);
    response.setCurrencyFrom(from);
    response.setCurrencyTo(to);
    response.setCurrencyValue(82);
    return new ResponseEntity<CurrencyResponse>(response, HttpStatus.OK);   
}

@GetMapping("/wish")
public ResponseEntity<String> wishMessage(){
    LocalDateTime ldt=LocalDateTime.now();
    int hour=ldt.getHour();
    String body=null;
    if(hour<12)
        body="Good Morning";
    else if(hour<16)
        body="Good Noon";
    else if(hour<20)
        body="Good evening";
    else
        body="Good Night";
    return new ResponseEntity<String>(body, HttpStatus.OK);
    
}

}

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>3.1.6</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>in.ineuron</groupId>
<artifactId>1.SpringRest-WebclientProducerApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>1.SpringRest-WebclientProducerApp</name>
<description>Webclient ProducerApp</description>
<properties>
    <java.version>17</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>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <image>
                    <builder>paketobuildpacks/builder-jammy-base:latest</builder>
                </image>
            </configuration>
        </plugin>
    </plugins>
</build>

</project>

测试请求URL

  • http://localhost:8081/1.SpringRest-WebclientProducerApp/api/currency/getCurrencyValue/from/USD/to/INR
  • http://localhost:8081/1.SpringRest-WebclientProducerApp/api/currency/wish

报错信息

HTTP Status 404 – Not Found,请求的资源[/1.SpringRest-WebclientProducerApp/api/currency/wish]不可用

排查与解决方法

  1. 检查启动类位置
    确保带@SpringBootApplication注解的启动类位于in.ineuron根包下,否则Spring无法自动扫描到控制器。若启动类在子包,需手动添加@ComponentScan(basePackages = "in.ineuron")注解。

  2. 确认context-path配置
    Spring Boot默认无context-path,URL中的/1.SpringRest-WebclientProducerApp可能是Tomcat自动添加的项目名。可在application.properties中显式配置:

    server.servlet.context-path=/1.SpringRest-WebclientProducerApp
    

    或直接去掉该部分,用http://localhost:8081/api/currency/wish测试。

  3. 验证端口正确性
    查看启动日志中的Tomcat started on port(s): 8081 (http)信息,确认应用确实运行在8081端口,避免端口冲突。

  4. 检查控制器注解
    确保@RestController、@RequestMapping、@GetMapping注解无拼写错误,路径配置符合预期。

  5. 清理重构项目
    执行mvn clean install清理缓存,重新构建后启动,避免编译或打包问题导致控制器未加载。


内容的提问来源于stack exchange,提问作者DhivyaViswanatha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 01:57:43