Search code examples
javaspark-framework

Spark Framework puts HTML around my response


I have been trying to make a REST API for my bot, using the Spark Framework, which I personally like alot, and I wouldn't switch, but when I try to publish a GSON encoded POJO, but it puts some HTML around it, making it unparseable by other applications. Here is the result in inspect element: the result

and what I wanted to be sent was just the part in the <pre> tag, as I expected from my code.

Just to make it a bit more clear on what Im trying to do, here are my Spark Route and POJO I am turning into 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);
    }
}

Solution

  • Check the output of your service with a tool like curl like so:

    curl http://localhost:8080/json
    

    Spark java does not wrap the answer in html, so check, where the output you are looking at and where you took the image from comes from.