I';我正在进行Mongodb和springboot的简单集成,但无法正确保存数据

I';我正在进行Mongodb和springboot的简单集成,但无法正确保存数据,mongodb,spring-boot,Mongodb,Spring Boot,我对java和spring boot相当陌生。我试图通过spring在mongo中保存数据,但它只保存_id=0和模型类。 我的控制器 package com.example.usermanagement.resource; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframe

我对java和spring boot相当陌生。我试图通过spring在mongo中保存数据,但它只保存_id=0和模型类。 我的控制器

package com.example.usermanagement.resource;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.usermanagement.model.User;
import com.example.usermanagement.repository.userRepository;

@RestController
public class UserController {
    
    @Autowired
    private userRepository repository;
    
    @PostMapping("/saveUser")
    public String saveUser(@RequestBody User user){
         System.out.println(user);
         repository.save(user); 
         return "User Added";
        
    }   
    @GetMapping("/findAllUsers")
    public List<User> getUsers(){
        return repository.findAll();
    }
    
    @GetMapping("/findAllUsers{id}")
    public Optional<User> getUser(@PathVariable int id){
        return repository.findById(id);
    }
    
    @DeleteMapping("/delete/{id}")
    public String deleteUser(@PathVariable int id){
        repository.deleteById(id);
        return "User Deleted";
    }   
    

}
On hitting save through postman, I get this in my db
[![enter image description here][1]][1]
以及存储库

package com.example.usermanagement.repository;

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

import com.example.usermanagement.model.User;

@Repository
public interface userRepository extends MongoRepository<User, Integer> {

}

当您不使用
@Field
通知数据库时,需要传递与参数相同的模型类名

{
    "id":2,
    "firstName":"yash",
    "lastName":"asd"
}

Lombok不会自动配置。因此,您需要手动配置

你有什么错误吗?如果在id字段中使用int,则必须增加id。不,我没有收到任何错误,post请求成功发生。但正如您在最后一张图像中看到的,只有id和类可见。另外,我正在发送post请求中的id。显示请求主体u sentI我已经编辑了我发送的请求的响应。很好。尝试
user.getFirstName(),user.getLastName()
{
    "id":2,
    "name":"yash",
    "lastName":"asd",
    "role":"dev"
}
{
    "id":2,
    "firstName":"yash",
    "lastName":"asd"
}