How to deal with null received from MongoDB using SpringBoot?

Viewed 46

I am using Java 17, Spring Boot 2.7 with MongoDB with MongoTemplate. But the data I am receiving are filled with null. Is the problem arises due to conversion from bson to json ? Or something else which I overlooked.

My UI Class:

@Getter
public class LvUi {
    private final String employeeCode;

    public LvUi(
        @Size(max = 5, message = "Employee Code Must Be Within 4 To 5 Character Long Or Blank")
        @JsonProperty("employeeCode") String employeeCode) {
        this.employeeCode = employeeCode;
    }
}

My Record Class:

public record Leave(
        String employeeCode,String leaveType,
        LocalDate startDate, LocalDate endDate
) {
}

My Repository:

public interface AttendanceRepo {
    List<Leave> selectLeaveApplication(LvUi lvUi);
}

@Repository("attendanceMongo")
public class AttendanceRepoMongo implements AttendanceRepo {

    private final MongoTemplate mongoTemplate;

    @Autowired
    public AttendanceRepoMongo(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    @Override
    public List<Leave> selectLeaveApplication(LvUi lvUi) {
        final Query query = new Query();
        query.addCriteria(Criteria.where("emp_cd")
                        .is(lvUi.getEmployeeCode()));
        query.fields()
        .include("emp_cd","leave_type","leave_start","leave_end");

return new ArrayList<>(mongoTemplate.find(query, Leave.class, "leave"));
    }
}

My Controller:

@RestController
@RequestMapping("hr/")
public class AttendanceApi {
    private final AttendanceService attendanceService;

    @Autowired
    public AttendanceApi(AttendanceService attendanceService) {
        this.attendanceService = attendanceService;
    }

    @PostMapping("leave/application/pre")
    public List<Leave> leaveApplicationPre(
            @Valid @NotNull @RequestBody LvUi lvUi) {
        return attendanceService.selectLeaveApplication(lvUi);
    }
}

My Service:

public interface AttendanceService {
    List<Leave> selectLeaveApplication(LvUi lvUi);
}

@Service("attendanceService")
public class AttendanceServiceImpl implements AttendanceService {
    private final AttendanceRepo attendanceRepo;

    @Autowired
    public AttendanceServiceImpl(
            @Qualifier("attendanceMongo") AttendanceRepo attendanceRepo) {
        this.attendanceRepo = attendanceRepo;
    }

    @Override
    public List<Leave> selectLeaveApplication(LvUi lvUi) {
        return attendanceRepo.selectLeaveApplication(lvUi);
    }
}

My Config:

@Configuration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
public class ErpConfig {

    @Bean
    public MongoTemplate mongoTemplate() {
        final MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
        return new MongoTemplate(mongoClient, "erpdb");
    }

}

My Data:

[
  {
    "_id": ObjectId("62ac28826940f44772b94b64"),
    "emp_cd": "A001",
    "appl_no": 59,
    "leave_type": "CL",
    "leave_start": ISODate("2022-05-09T00:00:00Z"),
    "leave_end": ISODate("2022-05-09T00:00:00Z")
  },
  {
    "_id": ObjectId("62ac28826940f55772b94b64"),
    "emp_cd": "A002",
    "appl_no": 69,
    "leave_type": "EL",
    "leave_start": ISODate("2022-05-09T00:00:00Z"),
    "leave_end": ISODate("2022-05-09T00:00:00Z")
  },
  {
    "_id": ObjectId("62ac28826940f66772b94b64"),
    "emp_cd": "A002",
    "appl_no": 79,
    "leave_type": "EL",
    "leave_start": ISODate("2022-06-09T00:00:00Z"),
    "leave_end": ISODate("2022-06-09T00:00:00Z")
  }
]

My Input (Through Postman):

POST
http://localhost:8080/hr/leave/application/pre
{
    "employeeCode": "A002"
}

My Output (Through Postman):

[
  {
    "emp_cd": null,
    "leave_type": null,
    "leave_start": null,
    "leave_end": null
  },
  {
    "emp_cd": null,
    "leave_type": null,
    "leave_start": null,
    "leave_end": null
  }
]

If I add the following code in my Repository

final FindIterable<Document> documents =
        mongoTemplate.getCollection("leave").find();
List<Document> res = new ArrayList<>();
for (Document document : documents)
    res.add(document);
res.forEach(System.out::println);

The Result :

Document{{_id=62ac28826940f44772b94b64, emp_cd=A001, appl_no=59, leave_type=CL, leave_start=Mon May 09 05:30:00 IST 2022, leave_end=Mon May 09 05:30:00 IST 2022}}
Document{{_id=62ac28826940f55772b94b64, emp_cd=A002, appl_no=69, leave_type=EL, leave_start=Mon May 09 05:30:00 IST 2022, leave_end=Mon May 09 05:30:00 IST 2022}}
Document{{_id=62ac28826940f66772b94b64, emp_cd=A002, appl_no=79, leave_type=EL, leave_start=Sat Jun 09 05:30:00 IST 2022, leave_end=Sat Jun 09 05:30:00 IST 2022}}

Please help.

0 Answers
Related