关于serviceComb中的swagger抛出NullPointerException

在使用serviceComb时, 如果抛出以下异常: 

 1 org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions >> Failed to setInstances, appId=hoolink-development, microserviceName=factory-rpc.
 2 java.lang.NullPointerException: null
 3     at org.apache.servicecomb.swagger.SwaggerUtils.validateSwagger(SwaggerUtils.java:80)
 4     at org.apache.servicecomb.core.definition.SchemaUtils.parseSwagger(SchemaUtils.java:49)
 5     at org.apache.servicecomb.core.definition.schema.ConsumerSchemaFactory.loadSwagger(ConsumerSchemaFactory.java:77)
 6     at org.apache.servicecomb.core.definition.schema.ConsumerSchemaFactory.createSchema(ConsumerSchemaFactory.java:54)
 7     at org.apache.servicecomb.core.definition.schema.ConsumerSchemaFactory.createSchema(ConsumerSchemaFactory.java:33)
 8     at org.apache.servicecomb.core.definition.schema.AbstractSchemaFactory.getOrCreateSchema(AbstractSchemaFactory.java:65)
 9     at org.apache.servicecomb.core.definition.schema.ConsumerSchemaFactory.createConsumerSchema(ConsumerSchemaFactory.java:47)
10     at org.apache.servicecomb.core.definition.MicroserviceVersionMeta.<init>(MicroserviceVersionMeta.java:40)
11     at org.apache.servicecomb.core.definition.MicroserviceVersionMetaFactory.create(MicroserviceVersionMetaFactory.java:38)
12     at org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions.lambda$setInstances$0(MicroserviceVersions.java:182)
13     at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
14     at org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx.computeIfAbsent(ConcurrentHashMapEx.java:56)
15     at org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions.setInstances(MicroserviceVersions.java:180)
16     at org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions.safeSetInstances(MicroserviceVersions.java:160)
17     at org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions.pullInstances(MicroserviceVersions.java:155)
18     at org.apache.servicecomb.serviceregistry.consumer.MicroserviceVersions.submitPull(MicroserviceVersions.java:127)
19     at org.apache.servicecomb.serviceregistry.consumer.MicroserviceManager.lambda$getOrCreateMicroserviceVersions$0(MicroserviceManager.java:55)
20     at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
21     at org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx.computeIfAbsent(ConcurrentHashMapEx.java:56)
22     at org.apache.servicecomb.serviceregistry.consumer.MicroserviceManager.getOrCreateMicroserviceVersions(MicroserviceManager.java:53)
23     at org.apache.servicecomb.serviceregistry.consumer.MicroserviceManager.getOrCreateMicroserviceVersionRule(MicroserviceManager.java:79)
24     at org.apache.servicecomb.serviceregistry.consumer.AppManager.getOrCreateMicroserviceVersionRule(AppManager.java:59)
25     at org.apache.servicecomb.edge.core.EdgeInvocation.findMicroserviceVersionMeta(EdgeInvocation.java:85)
26     at org.apache.servicecomb.edge.core.EdgeInvocation.edgeInvoke(EdgeInvocation.java:68)
27     at com.hoolink.gateway.build.dispatcher.ApiDispatcher.onRequest(ApiDispatcher.java:98)
28     at io.vertx.ext.web.impl.RouteImpl.handleContext(RouteImpl.java:223)
29     at io.vertx.ext.web.impl.RoutingContextImplBase.iterateNext(RoutingContextImplBase.java:101)
30     at io.vertx.ext.web.impl.RoutingContextImpl.next(RoutingContextImpl.java:120)
31     at org.apache.servicecomb.transport.rest.vertx.RestBodyHandler$BHandler.doEnd(RestBodyHandler.java:248)
32     at org.apache.servicecomb.transport.rest.vertx.RestBodyHandler$BHandler.end(RestBodyHandler.java:226)
33     at org.apache.servicecomb.transport.rest.vertx.RestBodyHandler.lambda$handle$0(RestBodyHandler.java:86)
34     at io.vertx.core.http.impl.HttpServerRequestImpl.handleEnd(HttpServerRequestImpl.java:418)
35     at io.vertx.core.http.impl.ServerConnection.handleLastHttpContent(ServerConnection.java:475)
36     at io.vertx.core.http.impl.ServerConnection.handleContent(ServerConnection.java:469)
37     at io.vertx.core.http.impl.ServerConnection.processMessage(ServerConnection.java:449)
38     at io.vertx.core.http.impl.ServerConnection.handleMessage(ServerConnection.java:156)
39     at io.vertx.core.http.impl.HttpServerImpl$ServerHandlerWithWebSockets.handleMessage(HttpServerImpl.java:705)
40     at io.vertx.core.http.impl.HttpServerImpl$ServerHandlerWithWebSockets.handleMessage(HttpServerImpl.java:614)
41     at io.vertx.core.net.impl.VertxHandler.lambda$channelRead$1(VertxHandler.java:150)
42     at io.vertx.core.impl.ContextImpl.lambda$wrapTask$2(ContextImpl.java:342)
43     at io.vertx.core.impl.ContextImpl.executeFromIO(ContextImpl.java:200)
44     at io.vertx.core.net.impl.VertxHandler.channelRead(VertxHandler.java:148)
45     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
46     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
47     at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
48     at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286)
49     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
50     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
51     at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
52     at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:310)
53     at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:284)
54     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
55     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
56     at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
57     at io.vertx.core.http.impl.Http1xOrH2CHandler.end(Http1xOrH2CHandler.java:49)
58     at io.vertx.core.http.impl.Http1xOrH2CHandler.channelRead(Http1xOrH2CHandler.java:27)
59     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
60     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
61     at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
62     at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286)
63     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
64     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
65     at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:340)
66     at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1434)
67     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:362)
68     at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:348)
69     at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:965)
70     at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
71     at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:645)
72     at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:580)
73     at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:497)
74     at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:459)
75     at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:884)
76     at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
77     at java.lang.Thread.run(Thread.java:748)

此堆栈信息表示在factory-rpc中有空的controller

所谓空的controller指的是: 该controller中没有任何方法

  当SwaggerUtils中的validateSwagger方法扫描到该controller, 

  然后通过swagger.getPaths()方法获取该controller中的函数

  由于controller中没有方法, 所以paths为null, 再使用paths.values(), 就会出现空指针

 1   public static void validateSwagger(Swagger swagger) {
 2     Map<String, Path> paths = swagger.getPaths();
 3     for (Path path : paths.values()) {
 4       Operation operation = path.getPost();
 5       if (operation != null) {
 6         List<Parameter> parameters = operation.getParameters();
 7         for (Parameter parameter : parameters) {
 8           if (BodyParameter.class.isInstance(parameter)) {
 9             if (((BodyParameter) parameter).getSchema() == null) {
10               throw new ServiceCombException("swagger validator: body parameter schema is empty.");
11             }
12           }
13         }
14       }
15     }
16   }
原文地址:https://www.cnblogs.com/yanwu0527/p/9881302.html