npe

yb3bgrhw  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(474)

我以webapi服务为例,尝试将其改编为基于mongo的后端。代码看起来几乎相同,只是我已经将transaction.class添加到user.class中(复制/重命名),以及服务实现和web处理程序。
我正在尝试从url/users/username获取用户json。mongo正确地检索它。
我的问题是:我有两段代码,一段工作(返回用户json),另一段不工作抛出npe。
不工作的代码(它编译,我不明白为什么不工作):

@Override
  public void getUser(
    String username,
    OperationRequest context, Handler<AsyncResult<OperationResponse>> resultHandler) {
      resultHandler.handle(
        persistence.getUser(username)
          .compose(user -> {
            if (user.isPresent()) {
              System.err.println("D: " + user.get());
              return Future.succeededFuture(OperationResponse.completedWithJson(user.get()));
            } else  {
              return Future.succeededFuture(new OperationResponse().setStatusCode(404).setStatusMessage("Not Found"));
            }
          })
      );
  }

它认为有以下例外:

SEVERE: Failed to handleMessage. address: __vertx.reply.1
java.lang.NullPointerException
        at io.vertx.ext.web.api.OperationResponseConverter.fromJson(OperationResponseConverter.java:15)
        at io.vertx.ext.web.api.OperationResponse.<init>(OperationResponse.java:30)
        at io.vertx.ext.web.api.contract.impl.RouteToEBServiceHandler.lambda$handle$1(RouteToEBServiceHandler.java:35)
        at io.vertx.core.eventbus.impl.EventBusImpl.lambda$convertHandler$2(EventBusImpl.java:342)
        at io.vertx.core.eventbus.impl.HandlerRegistration.deliver(HandlerRegistration.java:278)
...
...
D: {"_id":"5fbebeb97d3c4c0e48f9c7e0","email":"some@user.dk","firstName":"firstname","id":0,"lastName":"lastname","password":"password","phone":"+4512345678","username":"username"}

先抛出npe,然后抛出 System.err.println("D: " + user.get()); 执行并打印用户。浏览器将永远等待回复。
当这段代码正常工作时:

@Override
  public void getUser(
    String username,
    OperationRequest context, Handler<AsyncResult<OperationResponse>> resultHandler) {
      persistence.getUser(username).compose(user -> {
        if (user.isPresent()) {
          resultHandler.handle(Future.succeededFuture(OperationResponse.completedWithJson(user.get())));
          return Future.succeededFuture();
        } else  {
          resultHandler.handle(Future.succeededFuture(new OperationResponse().setStatusCode(404).setStatusMessage("Not Found")));
          return Future.failedFuture("Error");
        }
      });
  }

有人会假设将future.succeededfuture()返回给resulthandler.handle()是正确的方法?但是它把npe扔了!

kgsdhlau

kgsdhlau1#

我使用这个getuser()持久性方法实现了它:

@Override
  public Future<JsonObject> getUser(String username) {
    Promise<JsonObject> promise = Promise.promise();
    MongoClient mongoClient = MongoClient.createShared(vertx, mongoconfig);
    JsonObject query = new JsonObject().put("username", username);
    mongoClient.findOne(collectionName, query, null, promise::handle);
    return promise.future();
  }

以及此服务处理程序:

@Override
  public void getUser(
    String username,
    OperationRequest context, Handler<AsyncResult<OperationResponse>> resultHandler) {
      persistence.getUser(username)
        .onSuccess(jsonObject -> {
          resultHandler.handle(Future.succeededFuture(OperationResponse.completedWithJson(jsonObject)));
        });
  }

伟大的代码伴随着伟大的责任。。。一个人必须热爱未来(一旦理解):)

相关问题