我使用的是Guava的RateLimiter,所以我在API上没有达到每秒的最大请求数(5 ..)。它工作正常,我现在从未达到速率限制,但现在遇到了一个奇怪的问题。出于某种原因,它会在运行该应用程序的5次中有1次产生6个调用之一的异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.fasterxml.jackson.databind.JavaType.equals(java.lang.Object)' on a null object reference
                                               at com.fasterxml.jackson.databind.type.ResolvedRecursiveType.equals(ResolvedRecursiveType.java:103)
                                               at com.fasterxml.jackson.databind.type.TypeBindings$AsKey.equals(TypeBindings.java:458)
                                               at java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1014)
                                               at java.util.concurrent.ConcurrentHashMap.putIfAbsent(ConcurrentHashMap.java:1522)
                                               at com.fasterxml.jackson.databind.util.LRUMap.putIfAbsent(LRUMap.java:64)
                                               at com.fasterxml.jackson.databind.type.TypeFactory._fromClass(TypeFactory.java:1274)
                                               at com.fasterxml.jackson.databind.type.TypeFactory._fromParamType(TypeFactory.java:1384)
                                               at com.fasterxml.jackson.databind.type.TypeFactory._fromAny(TypeFactory.java:1154)
                                               at com.fasterxml.jackson.databind.type.TypeFactory._resolveSuperInterfaces(TypeFactory.java:1298)
                                               at com.fasterxml.jackson.databind.type.TypeFactory._fromClass(TypeFactory.java:1247)
                                               at com.fasterxml.jackson.databind.type.TypeFactory._fromAny(TypeFactory.java:1150)
                                               at com.fasterxml.jackson.databind.type.TypeFactory.constructType(TypeFactory.java:618)
                                               at com.fasterxml.jackson.databind.cfg.MapperConfig.constructType(MapperConfig.java:290)
                                               at com.fasterxml.jackson.databind.cfg.MapperConfig.introspectClassAnnotations(MapperConfig.java:320)
                                               at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.findTypeDeserializer(BasicDeserializerFactory.java:1338)
                                               at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:481)
                                               at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:3890)
                                               at com.fasterxml.jackson.databind.ObjectMapper._readValue(ObjectMapper.java:3756)
                                               at com.fasterxml.jackson.databind.ObjectMapper.readTree(ObjectMapper.java:2198)
                                               at dk.nykredit.jackson.dataformat.hal.deser.HALBeanDeserializer.deserialize(HALBeanDeserializer.java:27)
                                               at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
                                               at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2913)
                                               at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.readJavaType(MappingJackson2HttpMessageConverter.java:179)
                                               at org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.read(MappingJackson2HttpMessageConverter.java:174)
                                               at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:89)
                                               at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:738)
                                               at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:723)
                                               at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:544)
                                               at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:506)
                                               at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:455)
                                               at nl.app.App.API.RestClient_.getAdviseurs(RestClient_.java:117)
                                               at nl.app.App.Services.AdviseurService.getAdviseur(AdviseurService.java:60)
                                               at nl.app.App.Services.AdviseurService.getAdviseurAsync(AdviseurService.java:49)
                                               at nl.app.App.Services.AdviseurService_.access$101(AdviseurService_.java:19)
                                               at nl.app.App.Services.AdviseurService_$2.execute(AdviseurService_.java:66)
                                               at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:405)
                                               at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
                                               at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                               at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
                                               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)

最佳答案

该异常似乎与RateLimiter没有任何关系。堆栈跟踪表明,Spring和/或Jackson试图通过对.equals()引用的反射来调用null。我的猜测是您将null传递给了您不应该使用的东西-如果限速器没有足够的许可,也许您正在返回null

如果没有看到有问题的代码,很难确切地说出导致此错误的原因。尝试制作一个复制错误的MCVE-很可能只需创建MCVE就足以找到问题。

07-24 20:17