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

Spring Boot调用MongoDB返回200但数据未入库问题求助

Spring Boot接口返回200但数据未写入MongoDB排查

在Visual Studio的Thunder Client中调用Spring Boot的学生数据提交接口后,返回状态码200(OK),但数据并未写入MongoDB数据库。以下是完整代码及相关截图:

实体类:Student.java

package MongodbExample.Entity;

import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "students")
public class Students {
    
    private int id;
    private String name;
    private String city;
    private String college;

    public Students(int id, String name, String city, String college) {
        super();
        this.id = id;
        this.name = name;
        this.city = city;
        this.college = college;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCollege() {
        return college;
    }

    public void setCollege(String college) {
        this.college = college;
    }
}

仓库接口:StudentRepository.java

package MongodbExample.Repository;

import org.springframework.data.mongodb.repository.MongoRepository;

import MongodbExample.Entity.Students;

public interface StudentRepository extends MongoRepository<Students, Integer> {

}

控制器:StudentController.java

package MongodbExample.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
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 MongodbExample.Entity.Students;
import MongodbExample.Repository.StudentRepository;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentRepository studentRepo;
    
    @PostMapping("/")
    public ResponseEntity<?> addStudent(@RequestBody Students student) {
        Students save = this.studentRepo.save(student);
        return ResponseEntity.ok(save);
    }
    
    @GetMapping("/")
    public ResponseEntity<?> getStudent() {
        return ResponseEntity.ok(this.studentRepo.findAll());
    }
}

配置文件:application.properties

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=Spring_Testing

依赖文件: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.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mongodb.example</groupId>
    <artifactId>MongodbExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>MongodbExample</name>
    <description>This is mongodb practice connection</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

相关截图

  • API调用截图(Visual Studio Thunder Client):
    API调用截图
  • MongoDB Compass截图:
    MongoDB Compass截图

排查方案
  1. 补全实体类主键注解
    MongoDB需要明确的主键标识,你的Students类中id字段缺少@Id注解。Spring Data MongoDB依赖该注解识别文档主键,否则save操作无法正确完成持久化。修改实体类:
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "students")
public class Students {
    
    @Id
    private int id;
    // 其余字段与方法保持不变
}
  1. 验证MongoDB连接有效性
  • 执行mongosh命令确认本地MongoDB服务是否正常运行在localhost:27017
  • 查看Spring Boot启动日志,排查是否存在MongoDB连接失败的警告或错误
  1. 核对请求数据格式
    确认Thunder Client提交的JSON字段名与实体类字段完全匹配(大小写、名称一致)

  2. 开启MongoDB日志排查
    在application.properties中添加日志配置,查看save操作的详细执行日志:

logging.level.org.springframework.data.mongodb=DEBUG

重新调用接口后,通过控制台日志确认是否有插入语句执行记录或异常信息

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 11:47:52