0
@Route(...)
public Uni<?> call() {
    return Uni.createFrom().voidItem();
}

throws a NullPointerException: Invalid value returned by Uni: null

However

@Route(...)
public Uni<Void> call() {
    return Uni.createFrom().voidItem();
}

works perfectly fine and responds with HTTP 204

How do I manage to get either Uni<Void> or Uni<AnyObject> from the same method? I need to return http 204 only in specific scenarios


NOTE: This is only a partial answer, which resolves only the exception. However the C# code in the OP, even corrected based on this partial answer still has some inherent problem: It converges the NN to produce 0, 0, 0, 0 output (instead of the expected XOR rule 0, 1, 1, 0). I am posting this in hope it helps to iterate to the correct answer.


Maybe I am missing something, but my translation uses the batch size 4, while the original Python version somehow infers it being the training data in shape[2,4]

Anyway this causes the extra parameter batch_size with value 4 in fit() as I figured it out originally.

What I did not figured out, (and the resolution of the issue) is that this extra parameter batch_size with value 4 must be provided to model.predict() too, so instead the line

 print(model.predict(trainingData));

we should have

print(model.predict(trainingData, 4)); 

If anyone knows how to be more faithful to the original Python in regard using the API please correct me.

4

1 回答 1

1

您不能直接这样做,因为类型不同。我建议使用 RESTEasy Reactive 并执行以下操作:

@GET
public Uni<Response> call() {
   Uni<AnyObject> uni = .... ;
   return uni
      .onItem().transform(res -> {
        if (res == null) return Response.noContent().build();
        return Response.ok(res).build();
    });
}

通过发出一个Response对象,您可以自定义响应状态。

如果您想继续使用 Reactive Routes,另一种解决方案是不返回 a Uni,而是将RoutingContexta 作为参数获取:

@Route(...)
public void call(RoutingContext rc) {
   HttpServerResponse response = rc.response();
   Uni<AnyObject> uni = .... ;
   return uni
      .subscribe().with(res -> {
        if (res == null) response.setStatus(204).end();
        else response.setStatus(200).end(res);
    }, failure -> rc.fail(failure)); // will produce a 500.
}
于 2021-04-28T06:47:34.860 回答