我一直在阅读Micronaut文档,但是我找不到在回调中呈现http响应的方法,例如我可以使用Jax-Rs Jersey做到这一点。

在这里我想实现

@Get("/scalaFuture")
public void getScalaFuture() {
    Futures.successful(new SpringBootEntityDaoDTO())
            .onComplete(result -> {
                if (result.isSuccess()) {
                    return HttpResponse.ok(result.get());
                } else {
                    return HttpResponse.serverError(result.failed().get());
                }
            }, ExecutorContextUtil.defaultExecutionContext());
}


基本上在将来的回调中呈现响应。

类似于我在使用AsyncResponse的Observable回调中对JaxRS所做的事情

@POST
@Path("/bla")
public void foo(@Suspended final AsyncResponse asyncResponse) {
    Observable<EntityDaoDTO> observable = observableFosConnectorManager.execute("EntityAggregateRoot", "database", getEntityDaoDTO(), null, MethodDTO.CREATE);
    observable
            .subscribeOn(Schedulers.computation())
            .subscribe(result -> {
                EntityPayLoad entityPayLoad = new EntityPayLoad();
                entityPayLoad.setTitle(result.getTitle());
                entityPayLoad.setDescription(result.getDescription());
                asyncResponse.resume(Response.status(Response.Status.OK.getStatusCode()).entity(entityPayLoad).build());
            }, t -> asyncResponse.resume(Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build()),
            () -> getLogger().info(null, "Subscription done"));
}


问候

最佳答案

Micronaut允许不同的返回类型,包括反应性响应。

例如,您可以返回CompletableFuture

@Controller("/people")
public class PersonController {

    Map<String, Person> inMemoryDatastore = new ConcurrentHashMap<>();

    @Post("/saveFuture")
    public CompletableFuture<HttpResponse<Person>> save(@Body CompletableFuture<Person> person) {
        return person.thenApply(p -> {
                    inMemoryDatastore.put(p.getFirstName(), p);
                    return HttpResponse.created(p);
                }
        );
    }

}


将您的scala未来转换为Java可完成的未来:https://stackoverflow.com/a/46695386/2534803

https://docs.micronaut.io/latest/guide/index.html#_binding_using_completablefuture

09-04 06:28