我想根据 MongoDB 集合“processing-status”上的“listing status”和“createdOn”日期生成一个列表报告。 “listingStatus”字段属于嵌套集合。
ListingService方法如下:-
@Override
public List<ProcessingStatus> getProcessingStatusByListingStatusAndDate(String listingStatus,LocalDateTime startDate, LocalDateTime endDate) {
Query query = new Query();
logger.info("UTC Start Date: " + startDate);
logger.info("UTC End Date: " + endDate);
query.addCriteria(Criteria.where("bookscape.listingStatus").is(listingStatus));
query.addCriteria(Criteria.where("createdOn").gte(startDate).lte(endDate));
logger.info("Generated Query: " + query);
logger.info("Generated Query through Serialization: " + SerializationUtils.serializeToJsonSafely(query));
List<ProcessingStatus> ps = mongoTemplate.find(query, ProcessingStatus.class);
logger.info("Query result size " + ps);
return mongoTemplate.find(query, ProcessingStatus.class);
}
控制器”将使用以下方法调用服务方法:-
@GetMapping("/filter")public ResponseEntity<?> getProcessingStatusByNestedListingStatusAndDate(
@RequestParam String listingStatus,
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
logger.info("Method Parameters ");
logger.info("Listing Status: " + listingStatus);
logger.info("Start Date: " + startDate);
logger.info("End Date: " + endDate);
// Call the service method with the listing status and date range
List<ProcessingStatus> results =
listingReport.getProcessingStatusByListingStatusAndDate("Success", startDate, endDate);
// Output the results
System.out.println("Results: " + results.size());
if (results.isEmpty()) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(Collections.singletonMap("message", "No records
found matching the filter criteria."));
} else {
// Generate CSV report
byte[] reportBytes = generateCsvReport(results);
// Set headers for the CSV file download
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("filename",
"processing_statuses_report.csv");
return new ResponseEntity<>(reportBytes, headers, HttpStatus.OK);
}
}
private byte[] generateCsvReport(List<ProcessingStatus> processingStatuses) {
StringBuilder csvBuilder = new StringBuilder();
csvBuilder.append("Batch ID, ISBN, Listing Status, Created Date\n");
for (ProcessingStatus status : processingStatuses) {
String listingStatus = getStatusListing(status);
csvBuilder.append(status.getBatchId()).append(",")
.append(status.getIsbn()).append(",")
.append(listingStatus).append(",")
.append(status.getCreatedOn() != null ?
status.getCreatedOn().toString() : "").append("\n");
}
return csvBuilder.toString().getBytes(StandardCharsets.UTF_8);
}
private String getStatusListing(ProcessingStatus status) {
if (status.getAmazonEasyShip().getAmazonEasyShipDel().getListingStatus() != null)
return status.getAmazonEasyShip().getAmazonEasyShipDel().getListingStatus();
else if (status.getAmazonEasyShip().getAmazonEasyShipMum().getListingStatus() != null)
return status.getAmazonEasyShip().getAmazonEasyShipMum().getListingStatus();
else if (status.getAmazonFlex().getAmazonFlex().getListingStatus() != null)
return status.getAmazonFlex().getAmazonFlex().getListingStatus();
else if (status.getFlipkartHSP().getFlipkartHSP().getListingStatus() != null)
return status.getFlipkartHSP().getFlipkartHSP().getListingStatus();
else if (status.getFlipkartLSP().getFlipkartLSP().getListingStatus() != null)
return status.getFlipkartLSP().getFlipkartLSP().getListingStatus();
else if (status.getBookscape().getBookscape().getListingStatus() != null)
return status.getBookscape().getBookscape().getListingStatus();
else
return "";
}
输出(第一行因太长而断):-
[2024-06-11 18:08:27,148[0;39m [34mINFO [0;39m [[33mhttp-nio-8099-exec-1[0;39m] :
Method Parameters[37m2024-06-11 18:08:27,148[0;39m
[34mINFO [0;39m [[33mhttp-nio-8099-exec-1[0;39m] : Listing
Status: Success[37m2024-06-11 18:08:27,148[0;39m [34mINFO
[0;39m [[33mhttp-nio-8099-exec-1[0;39m] : Start Date:
2024-04-01T00:00[37m2024-06-11 18:08:27,148[0;39m [34mINFO
[0;39m [[33mhttp-nio-8099-exec-1[0;39m] : End Date:
2024-06-03T23:59:59[37m2024-06-11 18:08:27,148[0;39m [34mINFO
[0;39m [[33mhttp-nio-8099-exec-1[0;39m] : UTC Start Date:
2024-04-01T00:00[37m2024-06-11 18:08:27,149[0;39m [34mINFO
[0;39m [[33mhttp-nio-8099-exec-1[0;39m] : UTC End Date:
2024-06-03T23:59:59[37m2024-06-11 18:08:27,151[0;39m [34mINFO
[0;39m [[33mhttp-nio-8099-exec-1[0;39m] : Generated Query:
Query: { "bookscape.listingStatus" : "Success"}, Fields: {},
Sort: {}[37m2024-06-11 18:08:27,151[0;39m [34mINFO [0;39m
[[33mhttp-nio-8099-exec-1[0;39m] : Generated Query through
Serialization: { "$java" : Query: { "bookscape.listingStatus"
: "Success"}, Fields: {}, Sort: {} }[37m2024-06-11
18:08:27,327[0;39m [1;31mERROR[0;39m
[[33mhttp-nio-8099-exec-1[0;39m] : Servlet.service() for
servlet [dispatcherServlet] in context with path [] threw
exception [Request processing failed:
org.springframework.core.convert.ConversionFailedException:
Failed to convert from type [java.lang.String] to type
[java.util.Date] for value []] with root
causejava.lang.IllegalArgumentException: null
at java.base/java.util.Date.parse(Date.java:617)
at java.base/java.util.Date.<init>(Date.java:275)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
at org.springframework.core.convert.support.ObjectToObjectConverter.convert(ObjectToObjectConverter.java:113)
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:182)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:165)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.doConvert(MappingMongoConverter.java:1822)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.doConvert(MappingMongoConverter.java:1814)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:1350)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.getPotentiallyConvertedSimpleRead(MappingMongoConverter.java:1324)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$DefaultConversionContext.convert(MappingMongoConverter.java:2362)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$ConversionContext.convert(MappingMongoConverter.java:2168)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1934)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readProperties(MappingMongoConverter.java:620)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.populateProperties(MappingMongoConverter.java:537)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:514)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readDocument(MappingMongoConverter.java:480)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$DefaultConversionContext.convert(MappingMongoConverter.java:2359)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$ConversionContext.convert(MappingMongoConverter.java:2168)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter$MongoDbPropertyValueProvider.getPropertyValue(MappingMongoConverter.java:1934)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readProperties(MappingMongoConverter.java:620)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.populateProperties(MappingMongoConverter.java:537)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:514)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readDocument(MappingMongoConverter.java:480)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:416)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:412)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:119)
at org.springframework.data.mongodb.core.MongoTemplate$ReadDocumentCallback.doWith(MongoTemplate.java:3287)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2921)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2598)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2577)
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:866)
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:856)
at com.reproworks.ms.orchestrator.service.impl.ListingReportServiceImpl.getProcessingStatusByListingStatusAndDate(ListingReportServiceImpl.java:105)
at com.reproworks.ms.orchestrator.controller.MSPCalculationServiceController.getProcessingStatusByNestedListingStatusAndDate(MSPCalculationServiceController.java:773)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:354)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:716)
at com.reproworks.ms.orchestrator.controller.MSPCalculationServiceController$$SpringCGLIB$$0.getProcessingStatusByNestedListingStatusAndDate(<generated>)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:255)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:188)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:926)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:831)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1089)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
at org.springframework.web.filter.ServerHttpObservationFilter.doFilterInternal(ServerHttpObservationFilter.java:109)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1736)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63)
at java.base/java.lang.Thread.run(Thread.java:840)
我尝试在“ProcessingStatus”以及使用“createdOn”的任何地方使用“LocalDateTime”而不是“Date”字段。
我还尝试按如下方式进行UTC转换:-
// Convert the dates to UTC
ZoneId istZone = ZoneId.of("Asia/Kolkata");
ZoneId utcZone = ZoneId.of("UTC");
ZonedDateTime istStartDateTime
= ZonedDateTime.ofInstant(startDate.toInstant(), istZone);
ZonedDateTime istEndDateTime =
ZonedDateTime.ofInstant(endDate.toInstant(), istZone);
Date utcStartDate =
Date.from(istStartDateTime.withZoneSameInstant(utcZone).toInstant());
Date utcEndDate =
Date.from(istEndDateTime.withZoneSameInstant(utcZone).toInstant());
List<ProcessingStatus> processingStatuses =
listingReport.getProcessingStatusByListingStatusAndDate(
listingStatus, utcStartDate, utcEndDate);
logger.info("Listing Status: " + listingStatus);
logger.info("UTC Start Date: " + utcStartDate);
logger.info("UTC End Date: " + utcEndDate);
请建议如何解决此问题。
DateTimeFormat.ISO.DATE_TIME
表示时刻,是在特定的 UTC 偏移量中看到的日期(或者可能是时区,我不确定,因为我不使用 Spring ).
您选择使用
java.time.LocalDateTime
对象。该课程“不”代表一个时刻。该类仅表示具有当天时间的日期,但缺少与 UTC 或时区的偏移量的上下文。
代表一个时刻
LocalDateTime
。使用
Instant
、OffsetDateTime
或 ZonedDateTime
。