skip to Main Content

Here is my document and Employee is a collection in mongoDB database

@Document(collection = "Employee")
public class Employee {
    @Id
    private String id;
    private String eId;
    private String firstName;
    private String lastName;
    private String emailId;
    private int grpId;
}

This is the controller class

@RestController
@CrossOrigin(origins = "*")
public class EmployeeController {
    @Autowired
    private EmployeeService service;
    @GetMapping("/employee")
    public ResponseEntity<?> getAllEmployee(){
        List<Employee> employees;
        employees = service.getAllEmployees();
        return new ResponseEntity<List<Employee>>(employees, HttpStatus.OK);
    }
    @PostMapping("/employee")
    public void newEmployee(@RequestBody Employee employee){
        service.newEmployee(employee);
    }
}

This is the service class

@Service
public class EmployeeService {
    @Autowired
    private EmployeeRepository employeeRepo;
    public List<Employee> getAllEmployees(){
        return employeeRepo.findAll();
    }
    public void newEmployee(Employee employee){
        employeeRepo.save(employee);
    }
}

This is the repository Interface

@Repository
public interface EmployeeRepository extends MongoRepository<Employee,String> {
}

This is my post request to http://localhost:8080/employee
8080 is the port on which the application is running

{
        "eId": "01abc",
        "firstName": "your_firstName",
        "lastName": "your_lastName",
        "emailId": "[email protected]",
        "grpId": 1
}

Once the post request has been fulfilled when I use get request to fetch all the employees I get:

{
        "id": "63f6f0ffb84be0661b8d35a0",
        "firstName": "your_firstName",
        "lastName": "your_lastName",
        "emailId": "[email protected]",
        "grpId": 1,
        "eid": null
}

I am unable to figure out why eId is null if I am passing the eId as "01abc"

I tried searching the stack overflow for this but could not find a solution

2

Answers


  1. Ok, so according to your HTTP/POST request, I can see you’re not exclusively passing the ‘Id’ field as part of the JSON. So unless you’re auto-generating it somehow, here are some tips I could give to try out and see what works:

    1. If the id field is not part of your JSON, once the request body has been extracted in the controller, try and auto-generate an id. So your code could be something sort of
    @PostMapping("/employee")
        public void newEmployee(@RequestBody Employee employee){
            employee.setId(UUID.randomUUID().toString());
            service.newEmployee(employee);
        }
    
    1. It also helps to ensure all of your private members have getters and setters
    2. Also try and see that your class implements Serializable like this:
    public class Employee implements Serializable
    
    1. Final advice I can give is to avoid storing the JSON exactly as you send it in the HTTP/POST request(Because you never know what can happen to the JSOn as it is travelling across the web to your controller). This is where you may need to override you constructors to create a new object from the JSON once it hits your controller. So something like this:
    @PostMapping("/employee")
        public void newEmployee(@RequestBody Employee employee){
            Employee employeeToSave = new Employee(
               UUID.randomUUID.toString(), // for the ID field,
               employee.getEId(),
               .... // fill your class up with details from the JSON
            );
    
            // for sanity and confirmation, you can toString() this new object to ensure it's what you want to save
            log.info("Employee to save: {}", employeeToSave.toString());
            service.newEmployee(employeeToSave);
        }
    

    I hope one of these suggestions helps 🙂

    Login or Signup to reply.
  2. Change your id variable type to Long or UUID with @Generated and then
    change your post request to

    {
        "id" : null,
        "eId": "01abc",
        "firstName": "your_firstName",
        "lastName": "your_lastName",
        "emailId": "[email protected]",
        "grpId": 1
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search