Zack
Zack

Reputation: 13972

How do I serve json using spark's routing?

I am using spark to develop a small web application. I am coming off of using python's tornado framework, so a lot of this is new for me. I need to be able to serve json from the backend to the frontend. So, using their example, something like this.

import static spark.Spark.*;

public class HelloWorld {
    public static void main(String[] args) {
        get("/hello", (req, res) -> {
            return {
                "foo" : "bar"
            };
        });
    }
}

So to summarize, I want the ability to send a dictionary, a list, whatever is considered valid json to the frontend.

However, I understand that dicts/lists are not basic types in Java. Does anyone know how I could do this?

Upvotes: 0

Views: 2919

Answers (1)

Laercio Metzner
Laercio Metzner

Reputation: 1359

Take a look at the section Response Transformers at the documentation.

Upvotes: 1

Related Questions