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

Spring Boot API在Postman中请求返回404问题求助

Spring Boot POST请求404问题排查

我正在学习Spring Boot,使用Java 17、VS Code及Postman进行请求测试。这是一个用于学习Spring Boot基础的简单Demo,未配置实际数据库和前端,仅期望POST请求能返回200状态码,但发送POST请求至localhost:8080/api/v1/person(请求体为{"name": "John"})时出现404错误。

控制台日志

2023-02-27T21:55:37.208+02:00  INFO 10664 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 17.0.6 with PID 10664 (C:\Users\ASUS\Desktop\°≥Θσ≡σ· ∞⌠°σΘΘ≈ΦΘφ\Backend\demo\target\classes started by ASUS in C:\Users\ASUS\Desktop\°≥Θσ≡σ· ∞⌠°σΘΘ≈ΦΘφ\Backend\demo)
2023-02-27T21:55:37.214+02:00  INFO 10664 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2023-02-27T21:55:37.944+02:00  INFO 10664 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-02-27T21:55:37.955+02:00  INFO 10664 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-02-27T21:55:37.956+02:00  INFO 10664 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.5]
2023-02-27T21:55:38.057+02:00  INFO 10664 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-02-27T21:55:38.058+02:00  INFO 10664 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 802 ms
2023-02-27T21:55:38.344+02:00  INFO 10664 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-02-27T21:55:38.352+02:00  INFO 10664 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.44 seconds (process running for 1.756)
2023-02-27T21:55:44.377+02:00  INFO 10664 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-02-27T21:55:44.377+02:00  INFO 10664 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-02-27T21:55:44.378+02:00  INFO 10664 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

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.0.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</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>
    </dependencies>

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

</project>

相关代码文件

Person实体类

package com.example.demo.model;

import java.util.*;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Person {
          
      private final UUID id;
      private final String name;

      public Person(@JsonProperty("id") UUID id,
      @JsonProperty("name") String name) {
                  this.id = id;
                  this.name = name;
      }

      public UUID getId() {
                  return id;
      }

      public String getName() {
                  return name;
      }
}

PersonService类

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.example.demo.dao.PersonDao;
import com.example.demo.model.Person;

@Service
public class PersonService {

      private final PersonDao personDao;

      @Autowired
      public PersonService(@Qualifier("fakeDao") PersonDao personDao) {
                  this.personDao = personDao;
      }
      
      public int addPerson(Person person) {
                  return personDao.insertPerson(person);
      }
}

PersonController类

package com.example.demo.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.model.Person;
import com.example.demo.service.PersonService;

@RequestMapping("/api/v1/person")
@RestController
public class PersonController {
      
      private final PersonService personService;

      @Autowired
      public PersonController(PersonService personService) {
                  this.personService = personService;
      }

      @PostMapping
      public void addPerson(@RequestBody Person person) {
                  personService.addPerson(person);
      } 
}

Spring Boot启动类

package com.example.demo;

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

@SpringBootApplication
public class DemoApplication {

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

问题原因及解决方案

原因分析

PersonService依赖标注了@Qualifier("fakeDao")的PersonDao实现类,但代码中未提供该接口的具体实现,导致Spring容器无法创建PersonService的Bean,进而PersonController无法被正确初始化并注册到Spring MVC路由中,最终请求返回404。

解决步骤

  1. 创建PersonDao接口
    在com.example.demo.dao包下创建接口:
package com.example.demo.dao;

import com.example.demo.model.Person;

public interface PersonDao {
    int insertPerson(Person person);
}
  1. 实现PersonDao接口
    在com.example.demo.dao包下创建FakePersonDao实现类,添加@Repository("fakeDao")注解匹配@Qualifier名称:
package com.example.demo.dao;

import org.springframework.stereotype.Repository;
import com.example.demo.model.Person;

@Repository("fakeDao")
public class FakePersonDao implements PersonDao {
    @Override
    public int insertPerson(Person person) {
        // 模拟插入成功,返回1表示操作成功
        return 1;
    }
}
  1. 修改Controller方法返回200状态码
    原方法返回void会默认返回204 No Content,修改为返回200 OK:
@PostMapping
public ResponseEntity<Void> addPerson(@RequestBody Person person) {
    personService.addPerson(person);
    return ResponseEntity.ok().build();
}

完成修改后重启应用,再次发送POST请求即可得到200状态码。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 10:02:05