使用 WSResponse 时如何设置局部变量值

how to set local variable value, when use WSResponse

我正在尝试设置局部变量的值,但这显示错误

Local variable user_id defined in an enclosing scope must be final or effectively final

Local variable username defined in an enclosing scope must be final or effectively final

我该如何解决?

这是我的代码:

int user_id;
String username;

WSRequest req1 = ws.url("https://www.example.com");
CompletionStage<WSResponse> rsp1 = req1.get();

rsp1.thenAccept((rs) -> {
        JsonNode result = rs.asJson();
        Boolean status = result.findPath("status").asBoolean();
        if (status == true) {
            user_id = result.findPath("user").findPath("id").asInt(); //error here
            username = result.findPath("user").findPath("uname").textValue(); //error here
        }
    }).toCompletableFuture().get();

HTTP 请求是异步处理的,您不能从 lambda 函数内部(从另一个线程)设置外部作用域变量的值并以同步方式使用它。

如果要同步处理结果,只需要等待请求完成即可使用:

JsonNode result = rsp1.thenAccept(rs -> return rs.asJson()).toCompletableFuture().get();

Boolean status = result.findPath("status").asBoolean();
if (status) {
   ...
}