I'm doing simple integration of Mongodb and springboot, but unable to save data properly

Viewed 101

I'm fairly new to java and spring boot. I'm trying to save data in mongo through spring, but it only saves _id=0 and model class. My controller

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]

My model

package com.example.usermanagement.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString

@Document(collection = "user_data")
public class User {
    @Id
    private int id; 
    private String firstName;
    private String lastName;
}

And the repository

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

}

enter image description here

I do not understand what I'm doing wrong here, Why the rest of the data is not getting saved properly also Id is coming 0 rather than what I'm sending.

Post request I'm sending

{
    "id":2,
    "name":"yash",
    "lastName":"asd",
    "role":"dev"
}
1 Answers

When you dont use @Field to notify to database, you need to pass the same model class name as parameters.

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

Lombok won't automatically be configured. So you need to manually configure. Setting up lombok

Related