我一直在尝试为我的机器人制作一个 REST API,使用我个人非常喜欢的 Spark 框架,我不会切换,但是当我尝试发布 GSON 编码的 POJO 时,它会在它周围放置一些 HTML,使其无法被其他应用程序解析。这是检查元素中的结果:
正如我对代码所期望的那样,我想要发送的只是 <pre> 标记中的一部分。
只是为了让我更清楚地了解我想要做什么,这里是我的 Spark Route 和 POJO,我正在变成 JSON:
get("/json", (req, res) -> {
long sinceStart = System.currentTimeMillis() - Launcher.getInstance().getStartTime();
sinceStart /= 1000;
long hours = sinceStart / 3600;
sinceStart %= 3600;
long minutes = sinceStart / 60;
sinceStart %= 60;
res.type("application/json");
return new JSONResponse(hours + ":" + minutes + ":" + sinceStart,
Launcher.getInstance().getDispatcher().getDispatchCount(),
Launcher.getInstance().getDispatcher().getCommands().size(),
Launcher.getInstance().getClient().getGuilds().size());
});
...
private static class JSONResponse {
private String time;
private int commands, servers;
private long dispatchCount;
JSONResponse(String time, long dispatch, int registered, int servers) {
this.time = time;
this.dispatchCount = dispatch;
this.commands = registered;
this.servers = servers;
}
public String getTime() {
return time;
}
public int getCommands() {
return commands;
}
public int getServers() {
return servers;
}
public long getDispatchCount() {
return dispatchCount;
}
@Override
public String toString(){
return Launcher.getInstance().getGson().toJson(this);
}
}