如何使用Springboot处理从MongoDB收到的NULL?

发布于 2025-02-09 18:57:44 字数 4788 浏览 1 评论 0原文

我使用的是Java 17,带有Mongotemplate的MongoDB的Spring Boot 2.7。但是我收到的数据充满了空。问题是由于从BSON转换为JSON引起的吗?或我忽略的其他东西。

我的UI类:

@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;
    }
}

我的记录类:

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

我的存储库:

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"));
    }
}

我的控制器:

@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);
    }
}

我的服务:

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);
    }
}

我的配置:

@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");
    }

}

我的数据:

[
  {
    "_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")
  }
]

我的输入(通过Postman):

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

我的输出(通过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
  }
]

如果我在我的存储库中添加以下代码,则

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);

结果:

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

请帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文