我有以下查询来从集合中获取文档:
public List<Event> findSubEventsBetweenDates(Event parentEvent, LocalDate lowerDate, LocalDate upperDate) {
return mongo.find(query(
where("parentEvent").is(parentEvent)
.and("startDate").gte(lowerDate)
.and("startDate").lt(upperDate)
), Event.class);
}
抛出执行此异常时:
java.lang.RuntimeException: json can't serialize type : class org.joda.time.LocalDate
com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:77)
com.mongodb.util.JSONSerializers$MapSerializer.serialize(JSONSerializers.java:307)
com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79)
com.mongodb.util.JSON.serialize(JSON.java:56)
com.mongodb.util.JSON.serialize(JSON.java:41)
com.mongodb.BasicDBObject.toString(BasicDBObject.java:83)
java.lang.String.valueOf(String.java:2854)
java.lang.StringBuilder.append(StringBuilder.java:128)
org.springframework.data.mongodb.core.query.Criteria.setValue(Criteria.java:523)
org.springframework.data.mongodb.core.query.Criteria.getCriteriaObject(Criteria.java:470)
org.springframework.data.mongodb.core.query.Query.getQueryObject(Query.java:204)
org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:532)
org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:523)
net.wombaz.tango.model.dao.EventDAO.findSubEventsBetweenDates(EventDAO.java:114)
net.wombaz.tango.service.EventService.getSubEventsBetweenDates(EventService.java:289)
net.wombaz.tango.service.EventService$$FastClassByCGLIB$$d4d30570.invoke(<generated>)
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:639)
net.wombaz.tango.service.EventService$$EnhancerByCGLIB$$361caa89.getSubEventsBetweenDates(<generated>)
net.wombaz.tango.service.RepeatingEventService.updateRepeatingEventInstances(RepeatingEventService.java:76)
net.wombaz.tango.controller.EventController.editRepeatingEventPost(EventController.java:399)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:105)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
当未使用两个LocalDate标准之一时,不会发生此问题,例如
public List<Event> findSubEventsBetweenDates(Event parentEvent, LocalDate lowerDate, LocalDate upperDate) {
return mongo.find(query(
where("parentEvent").is(parentEvent)
.and("startDate").gte(lowerDate)
//.and("startDate").lt(upperDate)
), Event.class);
}
两者中的哪一个被注释掉都没关系。所以问题必须是,同一领域有两个标准。有人暗示我吗?
答案 0 :(得分:3)
使用
new Criteria().andOperator(
where("parentEvent").is(parentEvent),
where("startDate").gte(lowerDate),
where("startDate").lt(upperDate))
代替。
答案 1 :(得分:0)
如果您将LocalDate
更改为String,则会发现更明确的错误消息。
match(where("registerTime").gte(start.toString()).and("registerTime").lt(end.toString()))
然后在这种情况下错误消息是
org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'registerTime' expression specified as 'registerTime : { "$lt" : "2017-06-11"}'. Criteria already contains 'registerTime : { "$gte" : "2017-06-10"}'.
at org.springframework.data.mongodb.core.query.Criteria.setValue(Criteria.java:688)