我正在嘗試在 java Restful API 中運行 Get 方法。我嘗試將我的模型類命名為類似于我的實時 firebase。我嘗試使用介面回呼,它已列印到我的控制臺,但它仍然在我的郵遞員中回傳 null,
我的介面回呼類
public interface CallBack {
void onCallBack(DeviceTest value);
}
我的模型課
public class DeviceTest implements Serializable {
private String LightSwitch;
private String DoorSwitch;
// empty COnstructor
public DeviceTest() { }
public DeviceTest(String lightSwitch, String doorSwitch) {
this.LightSwitch = lightSwitch;
this.DoorSwitch = doorSwitch;
}
public String getLightSwitch() {
return LightSwitch;
}
public void setLightSwitch(String lightSwitch) {
LightSwitch = lightSwitch;
}
public String getDoorSwitch() {
return DoorSwitch;
}
public void setDoorSwitch(String doorSwitch) {
DoorSwitch = doorSwitch;
}
@Override
public String toString() {
return "DeviceTest{"
"LightSwitch='" LightSwitch '\''
", DoorSwitch='" DoorSwitch '\''
'}';
}
}
FireBase Connection 類和我使用的方法。
public static void handleLight(CallBack callback) {
FireBaseService fbs = null;
fbs = new FireBaseService();
device = new DeviceTest();
mylist = new ArrayList<Map<String, Object>>();
DatabaseReference ref = fbs.getDb()
.getReference("/Devices/Lamp/Ambient");
Map<String, Object> chemainChild = new HashMap<>();
// chemainChild.put("server/user/",array);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
callback.onCallBack(dataSnapshot.getValue(DeviceTest.class));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//mylist.add(device);
}
我用來列印給郵遞員的 Get 方法
DeviceTest deviceTest = new DeviceTest();
@GET
@Produces(MediaType.APPLICATION_JSON)
public DeviceTest getCustomers() {
//System.out.println(device.toString());
FireBaseService.handleLight(new CallBack() {
@Override
public void onCallBack(DeviceTest value) {
// this one will print to my console
System.out.println("Should work " value.toString());
// this value does not seem to be added to my deviceTest, likely a asynchronous issue again.
deviceTest = new DeviceTest(value.getLightSwitch(),value.getDoorSwitch());
}
});
// this is here i get my null value
return deviceTest ;
}
這是我在郵遞員那里得到的:

我在控制臺中得到了什么:

我的問題是如何DeviceTest{LightSwitch='LIGHT', DoorSwitch='CLOSED'} 在我的控制臺中將此值列印給我的郵遞員?問題似乎出在我的 GET 方法中。
uj5u.com熱心網友回復:
你是對的,抱歉誤解了這個問題。這是因為您deviceTest在解決其實際值之前立即回傳。
您可能想要使用 CompletableFuture。
嘗試這樣的事情:
CompletableFuture<DeviceTest> deviceTestFut = new CompletableFuture<>();
//System.out.println(device.toString());
FireBaseService.handleLight(new CallBack() {
@Override
public void onCallBack(DeviceTest value) {
// this one will print to my console
System.out.println("Should work " value.toString());
// this value does not seem to be added to my deviceTest, likely a asynchronous issue again.
deviceTestFut.complete(new DeviceTest(value.getLightSwitch(),value.getDoorSwitch());
})
});
// this is here i get my null value
return deviceTestFut;
您可以呼叫:
deviceTestFut.thenAccept(value -> System.out.println("Should work " value.toString()););
uj5u.com熱心網友回復:
解決了將 我的 GET 方法編輯為此
CompletableFuture<DeviceTest> deviceTestFut = new CompletableFuture<>();
//System.out.println(device.toString());
FireBaseService.handleLight(new CallBack() {
@Override
public DeviceTest onCallBack(DeviceTest value) {
deviceTest = new DeviceTest(value.getLightSwitch(),value.getDoorSwitch());
System.out.println("Here is the value" deviceTest.toString());
deviceTestFut.complete(new DeviceTest(value.getLightSwitch(),value.getDoorSwitch()));
return deviceTest;
}
});
// this is here i get my null value
deviceTestFut.thenAccept(value -> System.out.println("Should work " value.toString()));
return deviceTestFut.get();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/360654.html
